如何在垂直列表视图中的特定位置添加一组可水平滑动的项目?

时间:2014-09-23 07:33:22

标签: android listview horizontal-scrolling

将可滑动视图实现为垂直列表视图中的行项目的正确步骤是什么?

我尝试使用谷歌搜索,但我没有找到实现这个的好方法。

以下是我的要求的屏幕截图。

enter image description here

2 个答案:

答案 0 :(得分:1)

在我看来,处理此行为的正确方法是覆盖getViewTypeCount()getViewItemType(),后者应该返回普通类型和可滑动类型,可能是ViewPager。绘图部分并不那么棘手。我宁愿期待垂直/水平滚动

中的问题

答案 1 :(得分:0)

以下是使用ViewPagerListView的示例。 (似乎工作正常。) 我也在ListView中尝试过FragmentPagerAdapter和Fragments,但这产生了奇怪的结果。

<强> MainActivity.java

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final ListView listview = (ListView) findViewById(R.id.listView);
        MultiLayoutAdapter adapter = new MultiLayoutAdapter();
        adapter.addItem(new Content("First", Content.TYPE_TEXT));
        adapter.addItem(new Content("Second", Content.TYPE_TEXT));
        adapter.addItem(new Content("Imageset 1", Content.TYPE_IMAGESET));
        adapter.addItem(new Content("Third", Content.TYPE_TEXT));
        adapter.addItem(new Content("Imageset 2", Content.TYPE_IMAGESET));
        adapter.addItem(new Content("Fourth", Content.TYPE_TEXT));
        listview.setAdapter(adapter);
    }

    /* Simple class for multi-type content */
    private class Content {
        private static final int TYPE_TEXT = 0;
        private static final int TYPE_IMAGESET = 1;
        private static final int TYPE_COUNT = TYPE_IMAGESET + 1;
        private String mString;
        private int mType;
        public Content(String s, int type) {
            mString = s;
            mType = type;
        }
        public String getString() { return mString; }
        public int getType() { return mType; }
    }

    /* Adapter supporting multiple layouts */
    private class MultiLayoutAdapter extends BaseAdapter {
        private ArrayList<Content> mData = new ArrayList<Content>();
        private LayoutInflater mInflater;
        public MultiLayoutAdapter() {
            mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }
        public void addItem(final Content item) {
            mData.add(item);
            notifyDataSetChanged();
        }
        @Override
        public int getItemViewType(int position) { return mData.get(position).getType(); }
        @Override
        public int getViewTypeCount() { return Content.TYPE_COUNT; }
        @Override
        public int getCount() { return mData.size(); }
        @Override
        public String getItem(int position) { return mData.get(position).getString(); }
        @Override
        public long getItemId(int position) { return position; }
        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            View rowView = mInflater.inflate(R.layout.row_text, parent, false);
            /* TODO: Recycle convertView! */
            switch (getItemViewType(position)) {
                case Content.TYPE_TEXT:
                    rowView = mInflater.inflate(R.layout.row_text, parent, false);
                    TextView textView = (TextView) rowView.findViewById(R.id.textView);
                    textView.setText(mData.get(position).getString());
                    break;
                case Content.TYPE_IMAGESET:
                    rowView = mInflater.inflate(R.layout.row_imageset, parent, false);
                    ViewPager viewPager = (ViewPager) rowView.findViewById(R.id.viewPager);
                    viewPager.setAdapter(new ImagePagerAdapter(getApplicationContext()));
                    break;
            }
            return rowView;
        }
    }
}

<强> ImagePagerAdapter.java

public class ImagePagerAdapter extends PagerAdapter {
    int NumberOfPages = 3;
    LayoutInflater mInflater;
    int[] res = {
            android.R.drawable.ic_menu_camera,
            android.R.drawable.ic_menu_compass,
            android.R.drawable.ic_menu_directions};
    ImagePagerAdapter(Context c) {
        mInflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    @Override
    public int getCount() { return NumberOfPages; }
    @Override
    public boolean isViewFromObject(View view, Object object) { return view == object; }
    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        View v = mInflater.inflate(R.layout.image, container, false);
        ImageView imageView = (ImageView) v.findViewById(R.id.imageView);
        imageView.setImageResource(res[position]);
        container.addView(v);
        return v;
    }
    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((LinearLayout)object);
    }
}

以下是参考布局:

<强> activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:paddingLeft="16dp" android:paddingRight="16dp">
    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/listView" />
</RelativeLayout>

<强> row_text.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:padding="50dp"
        android:id="@+id/textView"/>
</LinearLayout>

<强> row_imageset.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <android.support.v4.view.ViewPager
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="100dp"/>
</LinearLayout>

image.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/imageView"/>
</LinearLayout>