Custom ViewPager

允我心安 提交于 2019-11-30 18:15:35

问题


How can i create a customized ViewPager ? To instantiate a page in the ViewPager it's something like this :

            public Object instantiateItem(View collection, int position) {
                    TextView tv = new TextView(cxt);
                    tv.setText("Bonjour PAUG " + position);
                    tv.setTextColor(Color.WHITE);
                    tv.setTextSize(30);

                    ((ViewPager) collection).addView(tv,0);

                    return tv;
            }

is there a way to inflate like this in a Class extended from an Adapter :

public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;
    if (v == null) {
        LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = vi.inflate(R.layout.the_layout, null);
    }
    CustomView cv = items.get(position);
    if (cv != null) {
            TextView text = (TextView) v.findViewById(R.id.text);
            if (text != null) {
                  ticker.setText(cv.getText()); 
            }

回答1:


This is a mere example, but it's working quiete well.

public Object instantiateItem(View collection, int position) {
        LayoutInflater inflater = (LayoutInflater)cxt.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View layout = inflater.inflate(R.layout.search_result, null);
        //
        TextView title = (TextView)layout.findViewById(R.id.search_result_title);
        TextView body = (TextView)layout.findViewById(R.id.search_result_body);
        TextView byline = (TextView)layout.findViewById(R.id.search_result_byline);
        TextView day = (TextView)layout.findViewById(R.id.search_result_date_day);
        TextView month = (TextView)layout.findViewById(R.id.search_result_date_month);
        TextView year = (TextView)layout.findViewById(R.id.search_result_date_year);
        //
        title.setText(list_title.get(position).toString());
        body.setText(list_body.get(position).toString());
        day.setText(list_day.get(position).toString());
        month.setText(list_month.get(position).toString());
        year.setText(list_year.get(position).toString());
        byline.setText(list_byline.get(position).toString());
        //
        ((ViewPager) collection).addView(layout,0);
        return layout;
    }



回答2:


Tsunaze is right. Also, there is another way of doing that. If you are using Fragments to set the UI View, you can use, getItem(int position), from FragmentPagerAdapter and then use onCreateView method from Fragment class.

    @Override
public Fragment getItem(int position) {
    // TODO Auto-generated method stub

    Log.d(TAG, "getItem");

    return ImageListFragment.newInstance(position, mList);
}



回答3:


Subclass the PagerAdapter class and override the corresponding methods.

Actually, if you look at the source code, you can only pass a PagerAdapter object to setAdapter() so the answer is no.



来源:https://stackoverflow.com/questions/6821320/custom-viewpager

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!