其他分享
首页 > 其他分享> > android-CardView示例不起作用

android-CardView示例不起作用

作者:互联网

我正在尝试android L版本中的两个新功能,

> RecyclerView
> CardView

好了RecyclerView对我来说很好用,但是当我尝试执行CardView时,它抛出一个错误,而我却找不到错误所在!

MainActivity.java

public class MainActivity extends Activity {

private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    String[] myDataset ={"Uday","Harihar","chetan","ravi","harish","Rahul","satish","vikaram","ravikiran","harish2"};

 mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);

    // improve performance if you know that changes in content
    // do not change the size of the RecyclerView
    mRecyclerView.setHasFixedSize(true);

    // use a linear layout manager
    mLayoutManager = new LinearLayoutManager(this);
    mRecyclerView.setLayoutManager(mLayoutManager);

    // specify an adapter (see also next example)
   mAdapter = new MyAdapter(myDataset);
    mRecyclerView.setAdapter(mAdapter); 

}

MyAdapter.java

package com.hrh.material;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private String[] mDataset;

// Provide a reference to the views for each data item
// Complex data items may need more than one view per item, and
// you provide access to all the views for a data item in a view holder
public static class ViewHolder extends RecyclerView.ViewHolder {
    // each data item is just a string in this case
    public TextView mTextView;
    public ViewHolder(View v) {
        super(v);
        mTextView = (TextView)v.findViewById(R.id.text);
    }
}

// Provide a suitable constructor (depends on the kind of dataset)
public MyAdapter(String[] myDataset) {
    mDataset = myDataset;
}

// Create new views (invoked by the layout manager)
@Override
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                               int viewType) {
    // create a new view
    View v = LayoutInflater.from(parent.getContext())
                           .inflate(R.layout.activity_card_view, parent, false);
    // set the view's size, margins, paddings and layout parameters

    ViewHolder vh = new ViewHolder(v);
    return vh;
}

// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    // - get element from your dataset at this position
    // - replace the contents of the view with that element
    holder.mTextView.setText(mDataset[position]);

}

// Return the size of your dataset (invoked by the layout manager)
@Override
public int getItemCount() {
    return mDataset.length;
}


}

activity_card_view.xml

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_height="match_parent"
android:layout_width="match_parent"
>

      <android.support.v7.widget.CardView
        android:id="@+id/cardview"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:elevation="100dp"
        card_view:cardCornerRadius="8dp"
        >

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

    </android.support.v7.widget.CardView>
</RelativeLayout>

在布局中显示“资源ID 0x7f040001不是STYLE类型(而是动画)”.

解决方法:

我可以按照以下步骤解决问题:

>从我的IDE(以Eclipse为例)中删除android-support-v7-appcompat和cardview libs项目.
>从Android SDK Manager更新Android SDK工具和Android支持库.
>重新启动IDE(Eclipse).
>导入android-support-v7-appcompat和cardview libs项目.

并确保您在xml文件的顶部添加了xmlns:card_view:

 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:card_view="http://schemas.android.com/apk/res-auto"

 <android.support.v7.widget.CardView
    android:id="@+id/card_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    card_view:cardCornerRadius="4dp">

就是这样,希望对您有所帮助.

标签:android-layout,android-cardview,android
来源: https://codeday.me/bug/20191121/2048605.html