Android CardView内部抽屉布局
当我将一些想法添加到列表中时,我有空白卡片:Android CardView内部抽屉布局,android,android-cardview,Android,Android Cardview,当我将一些想法添加到列表中时,我有空白卡片: list.add(new LocBean("ss", "dd", "aa")); 我哪里出错了? 下面是我的部分代码 我的onCreate方法: @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activ
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);
}
}
}
导入android.support.v7.widget.RecyclerView;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.TextView;
导入java.util.ArrayList;
公共类LocAdapter扩展了RecyclerView.Adapter{
数组列表;
公共本地适配器(ArrayList列表){
this.list=list;
}
@凌驾
public ViewHolder onCreateViewHolder(视图组父级,int-viewType){
视图v=LayoutInflater.from(parent.getContext()).flate(R.layout.loc\u View\u项,parent,false);
返回新的视图持有者(v);
}
@凌驾
公共无效onBindViewHolder(ViewHolder,int位置){
MyBean loc=list.get(位置);
holder.textName.setText(loc.getName());
holder.textLoc.setText(loc.getLoc());
holder.textRating.setText(loc.getRating());
}
@凌驾
public int getItemCount(){
返回list.size();
}
公共静态类ViewHolder扩展了RecyclerView.ViewHolder{
受保护的文本视图文本名称;
受保护的TextView textLoc;
受保护的文本视图文本评级;
公共视图持有者(视图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>
为适配器设置数据列表后,确保调用 notifyDataSetChanged(),以便适配器可以反映您的更改
adapter = new LocAdapter();
//将列表(list.add(newlocbean(“ss”、“dd”、“aa”);)传递到适配器
打电话
您可以选择whr来调用此方法,无论是您的活动还是您更改适配器数据的方法内部
有趣的编码:)IDRAWRLAYOUT您必须有2个元素,一个片段用于main,一个片段用于drawer。我知道RecyclerView是您抽屉布局中的第三个元素。您应该将RecycleView放在左侧菜单片段的xml文件中。如果notifydatasetchange()仍然不起作用,并且如果要使其起作用非常重要,您是否忘记调用notifydatasetchange(),在挂断处ping我djlegends33@gmail.com
adapter.notifyDataSetChanged()