使用绑定到ArrayList的CardView填充RecyclerView

时间:2015-07-26 22:32:53

标签: java android arrays android-recyclerview android-cardview

我正在尝试在RecyclerView中创建可刷卡的CardView列表。但是,当我运行应用程序时,CardViews中没有任何字符串值。

这是我的MainActivity类:

Project.config.to_h.each_with_object({}) do |(k,v), hsh|
   next unless k[/db_/]  # skip every key that doesn't match the regex (if you want the keys to not have part of them stripped then you can use an if/else along with the next line)
   hsh[k.to_s.sub(/db_/, "")] = v
end

这是我的activity_main.xml文件:

public class MainActivity extends Activity {


private CardViewAdapter mAdapter;

Button periodicbutton;

@Override
protected void onCreate(Bundle savedInstanceState) {
    periodicbutton = (Button) findViewById(R.id.periodbutton);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    String[] values = new String[] {"Quantitative Chemistry", "Atomic Structure", "Periodicity",
           "Bonding", "Energetics","Kinetics","Equilibrium","Acids & Bases","Oxidation & Reduction","Organic Chemistry"};

    final ArrayList<String> mItems = new ArrayList<>();
    for (int i = 0; i < values.length; i++) {
        mItems.add(values[i]);


    }

    OnItemTouchListener itemTouchListener = new OnItemTouchListener() {
        @Override
        public void onCardViewTap(View view, int position) {
            Toast.makeText(MainActivity.this, "Swipe me",     Toast.LENGTH_SHORT).show();
        }


    };
    mAdapter = new CardViewAdapter(mItems, itemTouchListener);

    RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);

    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    recyclerView.setAdapter(mAdapter);
    recyclerView.setHasFixedSize(false);
    SwipeableRecyclerViewTouchListener swipeTouchListener =
            new SwipeableRecyclerViewTouchListener(recyclerView,
                    new SwipeableRecyclerViewTouchListener.SwipeListener() {
                        @Override
                        public boolean canSwipe(int position) {
                            return true;
                        }

                        @Override
                        public void onDismissedBySwipeLeft(RecyclerView recyclerView, int[] reverseSortedPositions) {
                            for (int position : reverseSortedPositions) {
//                                    Toast.makeText(MainActivity.this, mItems.get(position) + " swiped left", Toast.LENGTH_SHORT).show();
                                mItems.remove(position);
                                mAdapter.notifyItemRemoved(position);
                            }
                            mAdapter.notifyDataSetChanged();
                        }

                        @Override
                        public void onDismissedBySwipeRight(RecyclerView recyclerView, int[] reverseSortedPositions) {
                            for (int position : reverseSortedPositions) {
//                                    Toast.makeText(MainActivity.this, mItems.get(position) + " swiped right", Toast.LENGTH_SHORT).show();
                                mItems.remove(position);
                                mAdapter.notifyItemRemoved(position);
                            }
                            mAdapter.notifyDataSetChanged();
                        }
                    });

    recyclerView.addOnItemTouchListener(swipeTouchListener);
}

public interface OnItemTouchListener {
    /**
     * Callback invoked when the user Taps one of the RecyclerView items
     *
     * @param view     the CardView touched
     * @param position the index of the item touched in the RecyclerView
     */
    void onCardViewTap(View view, int position);


}

public class CardViewAdapter extends RecyclerView.Adapter<CardViewAdapter.ViewHolder> {
    private List<String> cards;
    private OnItemTouchListener onItemTouchListener;

    public CardViewAdapter(List<String> cards, OnItemTouchListener onItemTouchListener) {
        this.cards = cards;
        this.onItemTouchListener = onItemTouchListener;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.card_layout, viewGroup, false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, int i) {

    }

    @Override
    public int getItemCount() {
        return cards == null ? 0 : cards.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {


        public ViewHolder(View itemView) {
            super(itemView);


            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onItemTouchListener.onCardViewTap(v, getPosition());
                }
            });
        }
    }
}


}

这是我正在尝试将字符串值绑定到:

的card_view.xml文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/background"
android:orientation="vertical"
android:scrollbars="vertical">

<android.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="fill_parent"
    android:layout_height="72dp"
    android:background="@color/primary">

</android.widget.Toolbar>

<android.support.v7.widget.RecyclerView
    android:id="@+id/recycler_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:scrollbars="vertical" >


    </android.support.v7.widget.RecyclerView>


</LinearLayout>

0 个答案:

没有答案