The list view in the clip does not scroll

There is a table content in my activity, which has three tabs. Each tab content has a fragment, and they are named onefragment, twofragment and thirdfragment. Now in my twofragment class, this class can handle the second tab content, which has a listview control

Question now: I don't know why the list control doesn't scroll!

This is my activity Code: call it foot_ Recipe class

package com.example.android.dezcook;

import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlException;
import android.database.sqlite.sqliteDatabase;
import android.os.Bundle;

import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class Foot_recipe extends AppCompatActivity {
    /*Layout Properties define*/
    private Toolbar toolbar;
    private TabLayout tabLayout;
    private ViewPager viewPager;
    private Food send_info;
    private int[] tabIcons = {
            R.drawable.tab_details_imagealbum,
            R.drawable.tab_details_cookie,
            R.drawable.tab_details_camera
    };
    /*End layout propeties DeFinition*/
    /*Start Logic deFinition from here*/

    /*End Logic DeFinition*/
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_foot_recipe);

        //get data from intent sended from cardview
        String txtID = "";
        Intent intent = getIntent();
        //   Bundle bundle=intent.getExtras();
        // if(bundle!=null)txtID=bundle.getString("txtid");
        Food food = (Food) intent.getParcelableExtra("food_obj");
        send_info = food;

        //tool bar customization
        TextView toolbar_title = (TextView) findViewById(R.id.toolbar_title);
        toolbar_title.setText(food.getTxtName());
        toolbarCustomization();
        //Tab customization
        tabCustomization();

        //Customize floatting button
        ActionButton_fav(food.getTxtid());

    }

    public void ActionButton_fav(final int id) {
        FloatingActionButton fav = (FloatingActionButton) findViewById(R.id.fav_btn_heart);
        fav.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (addDBfav(id) > 0)
                    Snackbar.make(v, "added to fav list", Snackbar.LENGTH_SHORT).show();
                else
                    Snackbar.make(v, "not in fav list", Snackbar.LENGTH_SHORT).show();
            }
        });
    }

    public Food sendFrag_info() {
        return send_info;
    }

    /*Layout Section
    * All Layout code about food details are down side this page
    * */
    private void toolbarCustomization() {
        toolbar = (Toolbar) findViewById(R.id.foodrestoolbar);
        setSupportActionBar(toolbar);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }


    private void tabCustomization() {
        viewPager = (ViewPager) findViewById(R.id.viewpager);
        setupViewPager(viewPager);
        tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(viewPager);
        setupTabIcons();
        tabLayout.getTabAt(2).select();
    }

    private void setupTabIcons() {
        tabLayout.getTabAt(0).setIcon(tabIcons[0]);
        tabLayout.getTabAt(1).setIcon(tabIcons[1]);
        tabLayout.getTabAt(2).setIcon(tabIcons[2]);

    }

    private void setupViewPager(ViewPager viewPager) {
        ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
        adapter.addFragment(new OneFragment(), "desc");
        adapter.addFragment(new TwoFragment(), "material");
        adapter.addFragment(new ThreeFragment(), "pic");
        viewPager.setAdapter(adapter);
    }

    class ViewPagerAdapter extends FragmentPagerAdapter {
        private final List<Fragment> mFragmentList = new ArrayList<>();
        private final List<String> mFragmentTitleList = new ArrayList<>();

        public ViewPagerAdapter(FragmentManager manager) {
            super(manager);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragmentList.get(position);
        }

        @Override
        public int getCount() {
            return mFragmentList.size();
        }

        public void addFragment(Fragment fragment, String title) {
            mFragmentList.add(fragment);
            mFragmentTitleList.add(title);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mFragmentTitleList.get(position);
        }

    }

    public int addDBfav(int id) {
        sqliteDatabase sqliteDatabase;
        DbHelper myDbHelper = new DbHelper(this);
/* try{
            myDbHelper.createDatabase();
        }catch (IOException e)
        {
            throw new Error("unable to create database");
    }*/
/*        myDbHelper.openDataBase();
        sqliteDatabase = myDbHelper.getWritableDatabase();
        Cursor cursor = sqliteDatabase.rawQuery("UPDATE food SET fav=1 WHERE food_id ==" + id + ";", null);
        Log.i("successfully:", Integer.toString(cursor.getCount()));
        return 1;*/

    myDbHelper.openDataBase();
        ContentValues values=new ContentValues();
        values.put("fav","1");
        myDbHelper.openDataBase();
        sqliteDatabase sqliteDatabase1=myDbHelper.getMyDataBase();
       int part= sqliteDatabase1.update("food",values,"food_id="+id,null);
        myDbHelper.close();
        Log.i("update","successful"+part);
        return  part;

    }

}

This is my twofragment class code:

Package com.example.android.dezcook;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.listadapter;
import android.widget.ListView;
import android.widget.TextView;

import com.example.android.dezcook.R;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;


public class TwoFragment extends Fragment {
    public TwoFragment()
    {
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View v = inflater.inflate(R.layout.fragment_two, container, false);

        Foot_recipe foot_recipe=(Foot_recipe)getActivity();

        Food food=foot_recipe.sendFrag_info();

      /*  ListView listView=(ListView)v.findViewById(R.id.show_items);
        listView.setAdapter();
*/      String[] arr=food.getItems().split("،");

        Log.i("LogArr",arr[0]);
      //  ArrayAdapter adapter = new ArrayAdapter<String>(getActivity(),R.layout.list_items_frag, arr);
        ListView listView = (ListView) v.findViewById(R.id.show_items);
        listView.setAdapter(new details_list_adapter(getActivity(),arr));
        return v;
    }

}

And twofragment XML code:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context="com.example.android.dezcook.TwoFragment"
    android:background="@drawable/gradient_background"
    >

    <!-- TODO: Update blank fragment layout -->

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/show_items"

        android:divider="@color/colorPrimaryDark"

        />

</LinearLayout>

Finally, it calls foot_ Activity of recipe XML code:

<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:background="@drawable/xml_header_background"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">


        <android.support.v7.widget.Toolbar
            android:id="@+id/foodrestoolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@drawable/xml_header_background"
            app:layout_scrollFlags="scroll|enterAlways"
            android:layout_gravity="right"
            app:title=" "

            android:gravity="right"

            >
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="foot specail"
                android:layout_gravity="right"
                android:textSize="25dp"
                android:textStyle="bold"
                android:layout_marginRight="10dp"
                android:textColor="#ffffff"
                android:id="@+id/toolbar_title" />

        </android.support.v7.widget.Toolbar>

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabMode="fixed"
            app:tabIndicatorColor="@color/cardview_dark_background"
            app:tabSelectedTextColor="@color/cardview_dark_background"
            app:tabGravity="fill"/>
    </android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"  />

    <android.support.design.widget.FloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/floating_btn_heart"
        android:layout_gravity="bottom|right"
        android:background="@color/colorPrimary"
        android:layout_margin="20dp"
        android:id="@+id/fav_btn_heart"
        />
</android.support.design.widget.CoordinatorLayout> 

resolvent:

Try adding the following code:

listView.setOnTouchListener(new ListView.OnTouchListener() {
                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                        int action = event.getAction();
                        switch (action) {
                            case MotionEvent.ACTION_DOWN:
                                events.
                                v.getParent().requestDisallowInterceptTouchEvent(true);
                                break;

                            case MotionEvent.ACTION_UP:
                                events.
                                 v.getParent().requestDisallowInterceptTouchEvent(false);
                                break;
                        }


                        v.onTouchEvent(event);
                        return true;
                    }
                });

Note: Android does not support nested scrolling, so you need to disable "parent" touch management and pass events to the view

I hope this helps

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>