DrawViewLayout中的CardView

时间:2017-04-27 23:53:04

标签: android android-cardview

当我在列表中添加一些想法时,我有空白卡片:

 list.add(new LocBean("ss", "dd", "aa"));

我犯错误的地方? 我的部分代码如下。

我的onCreate方法:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);

        new LoadAllLocs(this).execute(); //load list in background from database

        titles = getResources().getStringArray(R.array.loc_array);
        drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

        locRecycler = (RecyclerView) findViewById(R.id.recycler_view);
        locRecycler.setHasFixedSize(true);
        locLayoutManager = new GridLayoutManager(this, 1);
        locRecycler.setLayoutManager(locLayoutManager);
        list.add(new LocBean("ss", "dd", "aa"));

        locAdapter = new LocAdapter(list);
        locRecycler.setAdapter(LocAdapter);

我的适配器类:

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

import java.util.ArrayList;


public class LocAdapter extends RecyclerView.Adapter<LocAdapter.ViewHolder> {
    ArrayList<LocBean> list;
    public LocAdapter(ArrayList<LocBean> list){
        this.list= list;
    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.loc_view_item, parent, false);

        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        MyBean loc = list.get(position);
        holder.textName.setText(loc.getName());
        holder.textLoc.setText(loc.getLoc());
        holder.textRating.setText(loc.getRating());
    }

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

    public static class ViewHolder extends RecyclerView.ViewHolder{
        protected TextView textName;
        protected TextView textLoc;
        protected TextView textRating;
        public ViewHolder(View v){
            super(v);
            textName = (TextView) v.findViewById(R.id.name);
            textLoc = (TextView) v.findViewById(R.id.loc);
            textRating = (TextView) v.findViewById(R.id.rating);

        }
    }
}

loc_view_layout.xml:

<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    card_view:cardCornerRadius="4dp"
    android:padding="12dp"
    android:layout_margin="4dp">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/name"
            android:layout_alignParentTop="true"
            android:layout_alignParentStart="true"
            />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/loc"
            android:layout_below="@+id/name"
            android:layout_alignParentStart="true"
            />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/rating"
            android:layout_alignParentTop="true"
            android:layout_alignParentEnd="true"
            />

    </RelativeLayout>

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

和activity_maps.xml

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <!-- The main content view -->
    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    <!-- The navigation drawer -->
    <fragment xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:map="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.masaj.myapp.MapsActivity" />
    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:scrollbars="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice">
    </android.support.v7.widget.RecyclerView>
</android.support.v4.widget.DrawerLayout>

2 个答案:

答案 0 :(得分:2)

将dataList设置为适配器后,请确保拨打电话 notifyDataSetChanged()以便适配器可以反映您的更改

adapter = new LocAdapter();

//将您的列表(list.add(新的LocBean(“ss”,“dd”,“aa”));)传递给您的适配器 并致电

adapter.notifyDataSetChanged()

您可以选择调用此方法,无论是您的活动/在您修改适配器数据的方法中

玩得开心编码:)

答案 1 :(得分:0)

IDrawerLayout你必须有2个元素,一个片段用于main,一个用于抽屉。我看到RecyclerView是DrawerLayout中的第三个元素。你把RecyclerView放在左侧菜单片段的xml文件中。