我正在使用该liabray进行滑动手势。 https://github.com/nikhilpanju/RecyclerViewEnhanced
在MainAdapter.java类中,recyclerView包含List,但我想将其替换为List>类型。当我这样做时,会出现很多错误。请指导我如何做到这一点,或建议我做任何教程。

MainAdapter.java

public class MainAdapter extends RecyclerView.Adapter<MainAdapter.MainViewHolder> {
    LayoutInflater inflater;
    List<RowModel> modelList;

    public MainAdapter(Context context, List<RowModel> list) {
        inflater = LayoutInflater.from(context);
        modelList = new ArrayList<>(list);
    }

    @Override
    public MainViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.recycler_row, parent, false);
        return new MainViewHolder(view);
    }

    @Override
    public void onBindViewHolder(MainViewHolder holder, int position) {
        holder.bindData(modelList.get(position));
    }

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

    class MainViewHolder extends RecyclerView.ViewHolder {

        TextView mainText, subText;

        public MainViewHolder(View itemView) {
            super(itemView);
            mainText = (TextView) itemView.findViewById(R.id.mainText);
            subText = (TextView) itemView.findViewById(R.id.subText);
        }

        public void bindData(RowModel rowModel) {
            mainText.setText(rowModel.getMainText());
            subText.setText(rowModel.getSubText());
        }
    }
}

Activity :
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_admin_panel);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);


        unclickableRows = new ArrayList<>();
        unswipeableRows = new ArrayList<>();
        dialogItems = new String[25];
        for (int i = 0; i < 25; i++) {
            dialogItems[i] = String.valueOf(i + 1);
        }

        mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        mAdapter = new MainAdapter(this, getData());
        mRecyclerView.setAdapter(mAdapter);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));

        onTouchListener = new RecyclerTouchListener(this, mRecyclerView);
        onTouchListener
                .setIndependentViews(R.id.rowButton)
                .setViewsToFade(R.id.rowButton)
                .setClickable(new RecyclerTouchListener.OnRowClickListener() {
                    @Override
                    public void onRowClicked(int position) {
                        util.shortToast(getApplicationContext(), "Row " + (position + 1) + " clicked!");
                    }

                    @Override
                    public void onIndependentViewClicked(int independentViewID, int position) {
                        util.shortToast(getApplicationContext(), "Button in row " + (position + 1) + " clicked!");
                    }
                })
                .setSwipeOptionViews(R.id.add, R.id.edit, R.id.change)
                .setSwipeable(R.id.rowFG, R.id.rowBG, new RecyclerTouchListener.OnSwipeOptionsClickListener() {
                    @Override
                    public void onSwipeOptionClicked(int viewID, int position) {
                        String message = "";
                        if (viewID == R.id.add) {
                            message += "Add";
                        } else if (viewID == R.id.edit) {
                            message += "Edit";
                        } else if (viewID == R.id.change) {
                            message += "Change";
                        }
                        message += " clicked for row " + (position + 1);
                        util.shortToast(getApplicationContext(), message);
                    }
                });
        mRecyclerView.addOnItemTouchListener(onTouchListener);





        // Detect touched area
        detector = new Swipe(this,this);

        db = new DB(getApplicationContext());

        Bundle bundle = getIntent().getExtras();
        if(bundle != null) {
            String id = bundle.getString("id");
            Toast.makeText(this, "Welcome " + db.getUsernameById(id), Toast.LENGTH_LONG).show();
        }

        //get references of layout
//        adapter = new AdapterUsers(this, R.layout.textview_users, arrayList);
//        listView = (ListView) findViewById(R.id.users);
//        listView.setAdapter(adapter);
//
//        showAllUsers();
    }

    public void showAllUsers() {
        try {
            Cursor cursor = db.selectAllUsers();
            cursor.moveToFirst();
            adapter.clear();
            if (cursor.getCount() == 0) {
                Toast.makeText(this, "Empty user list", Toast.LENGTH_SHORT).show();
            } else {
                for (int i = 0; i < cursor.getCount(); i++) {
                    HashMap<String, Object> hm = new HashMap<String, Object>();
                    hm.put(ID, cursor.getString(cursor.getColumnIndex(db.ID_USER)));
                    hm.put(USERNAME, cursor.getString(cursor.getColumnIndex(db.NAME_USER)));
                    arrayList.add(hm);
                    cursor.moveToNext();
                }
            }
        } catch (Exception e) {
            Log.d("Show users", " failed due to " + e.getMessage());
        }
    }


    // Swipe interface
    @Override
    public void onSwipe(int direction) {
        String str = "";

        switch (direction) {

            case Swipe.SWIPE_RIGHT :
                listView.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View arg0) {
                        //deleteUser(id);
                        Toast.makeText(AdminPanel.this, "user deleted", Toast.LENGTH_LONG).show();
                    }
                });
                str = "Swipe Right";
                break;
            case Swipe.SWIPE_LEFT :  str = "Swipe Left";
                break;
            case Swipe.SWIPE_DOWN :  str = "Swipe Down";
                break;
            case Swipe.SWIPE_UP :    str = "Swipe Up";
                break;

        }
        Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onDoubleTap() {
        Toast.makeText(this, "Double Tap", Toast.LENGTH_SHORT).show();
    }


    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (touchListener != null) touchListener.getTouchCoordinates(ev);
        return super.dispatchTouchEvent(ev);
    }

    @Override
    public void setOnActivityTouchListener(OnActivityTouchListener listener) {
        this.touchListener = listener;
    }

    private List<RowModel> getData() {
        List<RowModel> list = new ArrayList<>(25);
        for (int i = 0; i < 25; i++) {
            list.add(new RowModel("Row " + (i + 1), "Some Text... "));
        }
        return list;
    }

}

最佳答案

您可以更改getData()方法并使其返回HashMap。

 private HashMap <String,String> getData() {
        HashMap <String,String> list = new HashMap <String,String>();
        for (int i = 0; i < 25; i++) {
            list.put(i,"Text");
        }
        return list;
    }

现在,在您的适配器中,您可以像...
public class MainAdapter extends RecyclerView.Adapter<MainAdapter.MainViewHolder> {
LayoutInflater inflater;
<HashMap<String,String> modelList;

public MainAdapter(Context context, <HashMap<String,String> modelList) {
    this.inflater = LayoutInflater.from(context);
    this.modelList = modelList;
}

@Override
public MainViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = inflater.inflate(R.layout.recycler_row, parent, false);
    return new MainViewHolder(view);
}

@Override
public void onBindViewHolder(MainViewHolder holder, int position) {
    //holder.bindData();
    holder.mainText.setText(modelList.get(position)); // value for the given key
}

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

class MainViewHolder extends RecyclerView.ViewHolder {

    TextView mainText, subText;

    public MainViewHolder(View itemView) {
        super(itemView);
        mainText = (TextView) itemView.findViewById(R.id.mainText);
        subText = (TextView) itemView.findViewById(R.id.subText);
    }

}
}

10-04 18:25