android垂直列表内的动态水平列表

时间:2019-07-06 06:51:20

标签: java android

我想在垂直列表的每个项目中创建一个动态高度的水平列表,但是我面临的问题是所有水平列表都是根据最后创建的水平列表的大小创建的。

我正在创建一个购物应用程序,其中必须根据类别显示产品。

下面是我的适配器类- 预先感谢。

public class vertical_proAdapter extends RecyclerView.Adapter<vertical_proAdapter.CatViewHolder> {
   private Context mContext;
   public static ArrayList<HashMap<String, String>> cat;
   CustomLoader loader;
   String id;
   DB db;
   SharedPreferences preference;
   ArrayList<HashMap<String, String>> subcategory = new ArrayList<>();


    public vertical_proAdapter(Context context, ArrayList<HashMap<String, String>> cat) {
       this.mContext = context;
       this.cat = cat;

    }

    @Override
    public CatViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.product_list_cstom_vertical, parent, false);
        loader = new CustomLoader(parent.getContext(), android.R.style.Theme_Translucent_NoTitleBar);
        preference = mContext.getSharedPreferences("Mypreference", mContext.MODE_PRIVATE);

        db = new DB(mContext);

        return new CatViewHolder(view);
    }

    @Override
    public void onBindViewHolder(CatViewHolder holder, int position) {
        holder.heading.setText(cat.get(position).get("category_name"));
        get_subcat(Integer.parseInt(cat.get(position).get("category_id")),holder.horizontal_list);

    }

    private void get_subcat(int s, RecyclerView horizontal_list) {

        Cursor cursor = null;
        subcategory.clear();
        String item_query = " SELECT * FROM " + DB.Table.Name.subcategory + " WHERE " + DB.Table.Name.s_category_id.toString()+" = "+ s;
        cursor = db.findCursor(item_query, null);

        if (cursor != null && cursor.moveToNext()) {

            for (int i = 0; i < cursor.getCount(); i++) {
                HashMap<String, String> map = new HashMap<>();

                map.put("category_id", cursor.getString(cursor.getColumnIndex(DB.Table.Name.s_category_id.toString())));
                map.put("subcategory_id", cursor.getString(cursor.getColumnIndex(DB.Table.Name.subcategory_id.toString())));
                map.put("subcategory_name", cursor.getString(cursor.getColumnIndex(DB.Table.Name.subcategory_name.toString())));
                map.put("subcategory_url", cursor.getString(cursor.getColumnIndex(DB.Table.Name.subcategory_url.toString())));

                subcategory.add(map);
                cursor.moveToNext();
            }
        }
        cursor.close();


        if (subcategory.size() > 0) {
            setAdapter(horizontal_list);

            ShowSnackbar("Data not found.");
        }


    }


    @Override
    public int getItemCount() {
        return  cat.size();
    }

    public class CatViewHolder extends RecyclerView.ViewHolder {

        private TextView heading;
        private RecyclerView horizontal_list;
        LinearLayoutManager linearLayoutManager;

        CatViewHolder(View itemView) {
            super(itemView);
            heading = (TextView)itemView.findViewById(R.id.heading);
            horizontal_list = (RecyclerView)itemView.findViewById(R.id.horizontal_list);

            linearLayoutManager =new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false);
            horizontal_list.setLayoutManager(linearLayoutManager);

        }

    }

    private void setAdapter(RecyclerView horizontal_list) {


        horizontal_proAdapter horizontal_proAdapter = new horizontal_proAdapter(mContext,subcategory);
        horizontal_list.setAdapter(horizontal_proAdapter);

    }



    public void ShowSnackbar(String msg) {
        // Create the Snackbar
        Snackbar snackbar = Snackbar.make(Dashboard.cl, "", Snackbar.LENGTH_LONG);
        // Get the Snackbar's layout view
        Snackbar.SnackbarLayout layout = (Snackbar.SnackbarLayout) snackbar.getView();
        // Hide the text
        TextView textView = (TextView) layout.findViewById(android.support.design.R.id.snackbar_text);
        textView.setVisibility(View.INVISIBLE);

        // Inflate our custom view

        LayoutInflater inflater = LayoutInflater.from(mContext);
        View snackView = inflater.inflate(R.layout.snackbar_custom, null);
        // Configure the view
        ImageView imageView = (ImageView) snackView.findViewById(R.id.image);
        TextView textViewTop = (TextView) snackView.findViewById(R.id.tv);
        textViewTop.setText(msg);
        textViewTop.setGravity(Gravity.CENTER);
        textViewTop.setTextColor(Color.WHITE);

        //If the view is not covering the whole snackbar layout, add this line
        layout.setPadding(10, 0, 0, 0);

        // Add the view to the Snackbar's layout
        layout.addView(snackView, 0);
        // Show the Snackbar
        snackbar.show();
    }

}

0 个答案:

没有答案