卡片视图没有出现

时间:2015-10-19 02:43:56

标签: android material-design

我正在通过android中的回收站视图实现卡片视图,但我的卡片视图没有显示出来。我使用自定义适配器将数据填充到卡片视图。我已经尝试了所有的东西,但卡片视图没有显示出来。

MainActivity.java:
public class MainActivity extends Activity {
private String[] mImage = {"sahil", "kunal", "somy", "manav"};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //setting adapter
    RecyclerView recyclerView = (RecyclerView)findViewById(R.id.recycler);
    GridLayoutManager manager = new GridLayoutManager(this, 2);
    recyclerView.setAdapter(new Adapter(mImage));
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(manager);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

}

activity_main.xml中:

<?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/recycler" android:scrollbars="vertical" tools:context=".MainActivity">

&#39;

grid_item.xml:

&#39;

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/text"/>

</LinearLayout>

&#39;

Adapter.java:
public class Adapter extends RecyclerView.Adapter<Adapter.Holder> {
private String[] mImage;

public Adapter(String[] mImage) {
    this.mImage = mImage;
}

public static class Holder extends RecyclerView.ViewHolder{
  public CardView cardView;
  public TextView textView;

    public Holder(View itemView) {
        super(itemView);
        cardView = (CardView)itemView.findViewById(R.id.card);
        textView = (TextView)itemView.findViewById(R.id.text);
    }
}

@Override
public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.grid_item, parent, false);
    Holder holder = new Holder(view);
    return holder;
}

@Override
public void onBindViewHolder(Holder holder, int position) {
    holder.textView.setText(mImage[position]);
}

@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
    super.onAttachedToRecyclerView(recyclerView);
}

@Override
public int getItemCount() {
    return 0;
}

}

1 个答案:

答案 0 :(得分:5)

getItemCount()方法更改为:

@Override
public int getItemCount() {
    return mImage.length;
}

现在你说你的适配器有0个项目,因此它没有绘制任何东西。