从listview项目单击打开视图寻呼机

时间:2015-03-24 08:58:46

标签: android listview android-fragments android-viewpager

我有一个包含json新闻的列表视图。
目前OnItemClickListener打开一项新活动,其中包含有关新闻的更多详细信息。 我想要的现在是使用viewpager 打开一项活动,该活动允许用户点击一个项目而不是点击新闻 返回列表视图以选择新项目。

然而,点击列表项应该打开相应的viewpager项目,例如,如果我点击列表视图中的项目3,我会看到一个带有项目3的新闻文章的寻呼机,然后我可以向前滑动到项目4,5,6等或返回项目2,1,0 以下是我目前的代码

有新闻列表的片段

public class Feed extends Fragment{
private List<DataModel> mDataList;
private class DataModel {
    private String sFeedTitle;
    private String sFeedDate;
    private String sFeedAuthor;
    private String sFeedTag;
    private String sFeedImage;
    private String sFeedImageAuthor;
    private String sFeed;

    // All getters and setters are here
    }
    @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
       Bundle savedInstanceState) { 
       View rootView = inflater.inflate(R.layout.news_list, container, false);
       return rootView;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);        
    super.onCreate(savedInstanceState);

    mListView = (ListView) getView().findViewById(R.id.ListView01);
    mProgress = (ProgressBar) getView().findViewById(R.id.progressBar);
    makeSampleHttpRequest();
    }

    private void makeSampleHttpRequest() {
    // Get data from the internet and set the model
    }

    private  class MyAdapter extends BaseAdapter {
     private LayoutInflater mInflater;

   public EfficientAdapter(Context context) {
       mInflater = LayoutInflater.from(context);
   }

   public int getCount() {
       return mDataList.size();
   }

   public Object getItem(int position) {
       return position;
   }

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

   public View getView(final int position, View convertView, ViewGroup parent) {
       ViewHolder holder;
        //All view holder code
         mListView.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1,
                    int pos, long arg3) {
                    Intent i = new Intent(getActivity(), ActivityFeed.class);
                    i.putExtra("FeedTitle", (mDataList.get(pos).getsFeedTitle()));
                    i.putExtra("FeedArticle", (mDataList.get(pos).getsFeed()));
                    i.putExtra("FeedImage", (mDataList.get(pos).getsFeedImage()));
                    i.putExtra("FeedAuthor", (mDataList.get(pos).getsFeedAuthor()));
                    i.putExtra("FeedDate", (mDataList.get(pos).getsFeedDate()));
                    startActivity(i);
                    // This is what is currently doing but instead of this i need to open an activity with a viewpager which allows swiping through all the news in the list. I am familiar with with implimenting a viewpager with static views an content like the one below
                }
       }
    }
}

包含静态视图的Viewpager 我如何修改它以使其符合我的需要

public class ViewPagerExample extends FragmentActivity {
private MyAdapter mAdapter;
private ViewPager mPager;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    mAdapter = new MyAdapter(getSupportFragmentManager());

    mPager = (ViewPager) findViewById(R.id.pager);
    mPager.setAdapter(mAdapter);
}

public static class MyAdapter extends FragmentPagerAdapter {
    public MyAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public int getCount() {
        return 3;
    }

    @Override
    public Fragment getItem(int position) {
        switch (position) {
        case 0:
            return new Fragment1();
        case 1:
            return new Fragment2();
        case 2:
            return new Fragment3();

        default:
            return null;
        }
    }
}
}

1 个答案:

答案 0 :(得分:0)

首先,您需要设置ViewPager页面的大小。例如,将其置于意图:

Intent i = new Intent(getActivity(), ActivityFeed.class);
i.putExtra("FeedTitle", (mDataList.get(pos).getsFeedTitle()));
i.putExtra("FeedArticle", (mDataList.get(pos).getsFeed()));
i.putExtra("FeedImage", (mDataList.get(pos).getsFeedImage()));
i.putExtra("FeedAuthor", (mDataList.get(pos).getsFeedAuthor()));
i.putExtra("FeedDate", (mDataList.get(pos).getsFeedDate()));
i.putExtra("PagesCount",mDataList.size());
startActivity(i);

在活动方面阅读它们:

int pagesCount = getIntent().getIntExtra("PagesCount");

初始化适配器。

mAdapter = new MyAdapter(getSupportFragmentManager(), pagesCount);

这是正确的初始化。但我认为你也需要一个数据。因此,请阅读Parcelable以将数据从一个Activity发送到另一个Activity。当你得到它 - 这里没有问题实现显示机制。