ListView横向滑动删除及点击事件

需求:listview横向滑动出现各种按钮,比如删除和点击事件。

方案:子控件使用HorizontalScrollView;

本人自定义功底不高,看到自定义listview就怕,所以选用HorizontalScrollView,这样都是直接应用系统的控件感觉简单;

要点:设置滑动之前的控件宽度为屏幕宽度,所以在onWindowFocusChanged计算屏幕宽度,然后初始化适配器;


代码如下:

主activity:

public class EBCActivity extends AppCompatActivity {

    private Context mContext;
    private List<Integer> mMapList = new ArrayList<>();
    private ListView mListView;
    private SimAdapter mSimAdapter;
    private int w1;//计算屏幕宽度

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.e_bb);
        mContext = this;
        mListView = (ListView) findViewById(R.id.lv1);
        mMapList.add(R.drawable.t1);
        mMapList.add(R.drawable.t2);
        mMapList.add(R.drawable.t3);
        mMapList.add(R.drawable.t4);

    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        Window window = getWindow();
        WindowManager windowManager = window.getWindowManager();
        w1 = windowManager.getDefaultDisplay().getWidth();
        //获取屏幕宽度后再设置适配器
        mSimAdapter = new SimAdapter();
        mListView.setAdapter(mSimAdapter);
    }

    public class SimAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return 50;
        }

        @Override
        public Object getItem(int position) {
            return mMapList.get(position % mMapList.size());
        }

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

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder;
            if (convertView == null) {
                viewHolder = new ViewHolder();
                convertView = LayoutInflater.from(mContext).inflate(R.layout.e_bc_item, null);
                viewHolder.l1 = (LinearLayout) convertView.findViewById(R.id.l1);
                viewHolder.t1 = (TextView) convertView.findViewById(R.id.t1);
                viewHolder.t2 = (TextView) convertView.findViewById(R.id.t2);
                LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) viewHolder.t1.getLayoutParams();
                //在onWindowFocusChanged后才获取屏幕宽度
                layoutParams.width = w1;
                viewHolder.t1.setLayoutParams(layoutParams);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            viewHolder.t2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    QiuUtil.ShortToast("删除:" + position);
                }
            });
            return convertView;
        }
    }

    class ViewHolder {
        LinearLayout l1;
        TextView t1;
        TextView t2;
    }
}


子item布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:padding="5dp">


    <HorizontalScrollView
        android:layout_width="match_parent"
        android:layout_height="100dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/t1"
                android:layout_width="500dp"
                android:layout_height="match_parent"
                android:background="@color/b"/>

            <TextView
                android:id="@+id/t2"
                android:layout_width="200dp"
                android:layout_height="match_parent"
                android:background="@color/y"/>
        </LinearLayout>
    </HorizontalScrollView>

</RelativeLayout>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值