需求: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>