我的ViewPager SetOnItemClickListener不起作用

时间:2018-02-28 14:07:40

标签: android listview android-viewpager

我在片段中有一个viewpager。我的viewpager有3个标签,每个标签都有一个列表视图。我想为列表视图的每个项目设置项目点击监听器以转到另一个片段,但我不能为第二个选项卡设置setOnItemClickListener&第三个标签,只是第一个标签工作。每个SetOnClickListener都是正确的。 请帮助我!

  

ViewPager XML

<android.support.v4.view.ViewPager
        android:id="@+id/vpPager"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <android.support.v4.view.PagerTabStrip
            android:id="@+id/pager_header"
            android:layout_width="match_parent"
            android:layout_height="36dp"
            android:layout_gravity="top"
            android:paddingBottom="4dp"
            android:textColor="@color/RakaarColor"
            android:paddingTop="4dp" />

    </android.support.v4.view.ViewPager>
  

ViewPager类

public class ProjectsFragment extends Fragment  {

MyPagerAdapter adapterViewPager;

public static class MyPagerAdapter extends FragmentPagerAdapter  {
    private static int NUM_ITEMS = 3;
    String[] titles = {"MyWorks", "SentBids", "ExistProject"};

    public MyPagerAdapter(FragmentManager fragmentManager) {
        super(fragmentManager);
    }

    // Returns total number of pages
    @Override
    public int getCount() {
        return NUM_ITEMS;
    }

    // Returns the fragment to display for that page
    @Override
    public Fragment getItem(int position) {
        switch (position) {
            case 0: // Fragment # 0 - This will show FirstFragment
                return MyWorksFragment.newInstance(0);
            case 1: // Fragment # 0 - This will show FirstFragment different title
                return SentBidsFragment.newInstance(1);
            case 2: // Fragment # 1 - This will show SecondFragment
                return ExistProjectsFragment.newInstance(2);
            default:
                return null;
        }
    }

    // Returns the page title for the top indicator
    @Override
    public CharSequence getPageTitle(int position) {
        return titles[position];
    }

}

public ProjectsFragment() {
    // Required empty public constructor
}


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

    ViewPager vpPager = (ViewPager) view.findViewById(R.id.vpPager);
    adapterViewPager = new MyPagerAdapter(getChildFragmentManager());
    vpPager.setAdapter(adapterViewPager);
    vpPager.setOffscreenPageLimit(0);
    vpPager.setCurrentItem(3);

    PagerTabStrip pagerTabStrip = (PagerTabStrip) view.findViewById(R.id.pager_header);
    pagerTabStrip.setDrawFullUnderline(true);
    pagerTabStrip.setTabIndicatorColor(Color.parseColor("#44b1c5"));

    Typeface IRANSansFaNum = Typeface.createFromAsset(getActivity().getAssets(), "fonts/IRANSansMobile.TTF");

    for (int i = 0; i < pagerTabStrip.getChildCount(); ++i) {
        View nextChild = pagerTabStrip.getChildAt(i);
        if (nextChild instanceof TextView) {
            TextView textViewToConvert = (TextView) nextChild;
            textViewToConvert.setTypeface(IRANSansFaNum);
        }
    }

    Button plus = (Button)view.findViewById(R.id.projects_plus_button);
    plus.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            AddFragment Add = new AddFragment();
            FragmentManager manager = getActivity().getSupportFragmentManager();
            manager.beginTransaction().replace(R.id.mycontainer, Add).addToBackStack("Back").commit();
        }
    });
    return view;
}
  

我的适配器的一个例子

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_exist_projects, container, false);

    ProjectsAdapter madapter;
    plistnewsData.clear();
ListView exist_projects_listview = (ListView)view.findViewById(R.id.exist_projects_listview);
    plistnewsData.add(new ProjectsAdapterItems("Name", "Details", "Cost", "Day", "Date", "120 Bids"));
madapter = new ProjectsAdapter(plistnewsData);
    exist_projects_listview.setAdapter(madapter);

    exist_projects_listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        ProjectsItemsFragment Add = new ProjectsItemsFragment();
        FragmentManager manager = getActivity().getSupportFragmentManager();
        manager.beginTransaction().replace(R.id.mycontainer, Add).addToBackStack("Back").commit();

        SharedPreferences pos = getActivity().getSharedPreferences("position", Context.MODE_PRIVATE);
        SharedPreferences.Editor posEdit = pos.edit();
        posEdit.putString("position", "" + position);
        posEdit.commit();
    }
});

    return view;

}

private class ProjectsAdapter extends BaseAdapter {
    public ArrayList<ProjectsAdapterItems> plistnewsDataAdpater ;

    public ProjectsAdapter(ArrayList<ProjectsAdapterItems>  plistnewsDataAdpater) {
        this.plistnewsDataAdpater=plistnewsDataAdpater;
    }

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

    @Override
    public String getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        LayoutInflater mInflater = getActivity().getLayoutInflater();
        View myView = mInflater.inflate(R.layout.title_of_firstlistview, null);

        final ProjectsAdapterItems p = plistnewsDataAdpater.get(position);

        TextView name = (TextView)myView.findViewById(R.id.projects_name);
        name.setText(p.pname);

        TextView details = (TextView)myView.findViewById(R.id.projects_details);
        details.setText(p.pdetails);

        TextView cost = (TextView)myView.findViewById(R.id.projects_cost);
        cost.setText(p.pcost);

        TextView day = (TextView)myView.findViewById(R.id.projects_day);
        day.setText(p.day);

        TextView date = (TextView)myView.findViewById(R.id.projects_date);
        date.setText(p.date);

        TextView bids = (TextView)myView.findViewById(R.id.project_propsal);
        bids.setText(p.bids);

        return myView;
    }

}

}

0 个答案:

没有答案