Listview example of horizontal sliding (horizontal sliding) in Android

Use of horizontal listview horizontallistview

In Android, the listview slides vertically by default. Due to the needs of the project, the listview needs to slide horizontally. There are many ways to implement it, but a better way is to encapsulate a control by yourself. The use method is the same as that of listview. What needs to be improved: the obtained image size is not processed. The original size of the picture is displayed on the interface. For better display effect, it should be compressed into a unified size.

The code of horizontallistview.java is as follows:

The adapter horizontallistviewadapter. Java is as follows:

public class horizontallistViewAdapter extends BaseAdapter {
  /** 上下文 */
  private Context mContext;
  /** 图像数据源 */
  private ArrayList<Map<String,Integer>> mImageList;

  /** 数据源 */
  private ArrayList<Map<String,Integer>> mTextList;
  /** Image */
  private static String IMAGE = "ic_";

  private Map<String,Integer> mMap = null;

  /** 构造方法 */
  public horizontallistViewAdapter(Context context) {
    this.mContext = context;
    initData();
  }

  /** 初始化数据 */
  public void initData() {
    mImageList = new ArrayList<Map<String,Integer>>();
    /*
     * 反射技术
     */
    Class<?> imageClzz = R.drawable.class;
    R.drawable instance = new R.drawable();
    // 取得drawable类中所有的字段
    Field[] fields = imageClzz.getDeclaredFields();
    for (Field field : fields) {
      // 获得字段的名字
      String name = field.getName();
      if (name != null && name.startsWith(IMAGE)) {
        try {
          mMap = new HashMap<String,Integer>();
          mMap.put(IMAGE,(Integer) field.get(instance));
          mImageList.add(mMap);
        } catch (illegalaccessexception e) {
          e.printStackTrace();
        }
      }

    }
  }


  @Override
  public int getCount() {
    return mImageList.size();
  }

  @Override
  public Map<String,Integer> getItem(int position) {

    return mImageList == null ? null : mImageList.get(position);
  }

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

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

    ViewHolder holder;
    if (convertView == null) {
      holder = new ViewHolder();
      convertView = LayoutInflater.from(mContext).inflate(
          R.layout.horizontal_list_item,null);
      holder.mImage = (ImageView) convertView
          .findViewById(R.id.iv_list_item);
      holder.mTitle = (TextView) convertView
          .findViewById(R.id.tv_list_item);
      convertView.setTag(holder);
    } else {
      holder = (ViewHolder) convertView.getTag();
    }

    if (position == mSelectIndex) {
      convertView.setSelected(true);
    } else {
      convertView.setSelected(false);
    }
    holder.mImage.setImageResource(getItem(position).get(IMAGE));
    return convertView;
  }

  private class ViewHolder {
    /** 图像 */
    private ImageView mImage;
  }
}

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>