在列表视图项的Andr​​oid水平滚动型

在列表视图项的Andr​​oid水平滚动型

本文介绍了在列表视图项的Andr​​oid水平滚动型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表视图与20行,我想设置一个水平滚动视图在列表视图中的每一行项目,因为每个行包含多个项目。

I have a list view with 20 rows and I want to setup a horizontal scrollview for every row item in list view, as each row contains more than one item.

下面是我的code:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <HorizontalScrollView
        android:id="@+id/hor_scroll"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scrollbars="none" >

        <LinearLayout
            android:id="@+id/mainLinear"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >
        </LinearLayout>
    </HorizontalScrollView>

</RelativeLayout>

内排布局是一排被复制任何次数

<ImageView
    android:id="@+id/icon"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_marginRight="6.0dip"
    android:src="@drawable/ic_launcher" />

<TextView
    android:id="@+id/text"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:gravity="center_vertical"
    android:text="Example Value"
    android:textAppearance="?android:textAppearanceMedium" />

BaseAdapter

public class HorizontalListViewAdapter extends BaseAdapter {

    private Context context;
    private ArrayList<ArrayList<DwivediJi>> dataSet;


    public HorizontalListViewAdapter(Context context) {
        this.context = context;
    }

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

    @Override
    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return arg0;
    }

    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return arg0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.horizontal_list_item, null,false);

        LinearLayout mainLinnerLayout=(LinearLayout)convertView.findViewById(R.id.mainLinear);

         for (int i = 0; i <5; i++) {
             View additionView = inflater.inflate(R.layout.inner_layout_file, null,false);
             LinearLayout innerLinnerLayout=(LinearLayout)additionView.findViewById(R.id.inner_layout);
             mainLinnerLayout.addView(innerLinnerLayout);
        }
        return convertView;
    }

    class ViewHolder {
        TextView tv_titleExample;
        HorizontalScrollView hzView;
        LinearLayout linear_layout,main_linear_layout;
    }
}

我的问题

请看附加的屏幕截图。我的问题是,一个以上的观点是示出在同一时间在每行中。

Look at the screenshot attached. My problem is that more than one view is showing at one time in each row.

我想,只有一个观点应该展现给用户在同一时间,为所有的用户所要做的就是刷卡从左到右或从右到左的其余部分。

I want that only one view should show to the user at one time and for the rest all the user has to do is swipe left to right or right to left.

推荐答案

请注意:不是一个理想的解决方案,但应该提供你想要的..另注:这可能会让你的ListView略有janky,根据布局的复杂性。

Note: Not an ideal solution, but should provide what you want..Another Note: This may make your listview slightly janky, depending on the layout-complexity

@Override
    public View getView(int position, View convertView, ViewGroup parent) {

        LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.horizontal_list_item, null,false);

        LinearLayout mainLinnerLayout=(LinearLayout)convertView.findViewById(R.id.mainLinear);

         for (int i = 0; i <5; i++) {
             View additionView = inflater.inflate(R.layout.inner_layout_file, null,false);
             LinearLayout innerLinnerLayout=(LinearLayout)additionView.findViewById(R.id.inner_layout);

             // If the width varies for each innerLinnerLayout, then remove the if block & always calculate padding value
             // padding is an integer initialized to -1 in the constructor
             if (padding == -1) {
                 int width = context.getResources().getDisplayMetrics().widthPixels;
                 innerLinnerLayout.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
                 padding = width - additionView.getMeasuredWidth();
             }
             // I've set padding to right only, but you could center it by giving left and right padding of value=(padding/2)
             innerLinnerLayout.setPadding(0, 0, padding, 0);
             mainLinnerLayout.addView(innerLinnerLayout);
        }
        return convertView;
    }

这篇关于在列表视图项的Andr​​oid水平滚动型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 01:02