我想创建一个显示列表的应用程序(RecyclerView)。我想在此RecyclerView的Viewholder中包含一个TextView。
MainActivity代码:
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private CustomAdapter customAdapter;
ListView listView;
public Cursor cursor;
public StudentRepo studentRepo;
private final static String TAG = MainActivity.class.getName().toString();
private static final String TAG_BOOKMARKS="bookmarks";
private static final String TAG_ABOUT_US="about";
//recyclerView implementation
private List<TopSample> wordlist=new ArrayList<>();
private RecyclerView recyclerView;
private StudentAdapter studentAdapter;
//recyclerView implementation done
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
studentRepo = new StudentRepo(this);
//recyclerView implement
recyclerView=(RecyclerView)findViewById(R.id.recycler_view);
recyclerView.setHasFixedSize(true);
DatabaseAccess databaseAccess = DatabaseAccess.getInstance(this);
databaseAccess.open();
Cursor cursor= databaseAccess.getInfo();
cursor.moveToFirst();
do{
TopSample topSample=new TopSample(cursor.getString(0));
wordlist.add(topSample);
}while (cursor.moveToNext());
databaseAccess.close();
studentAdapter=new StudentAdapter(wordlist);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(mLayoutManager);
//recyclerView.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(studentAdapter);
StudentAdapter类
public class StudentAdapter extends RecyclerView.Adapter<StudentAdapter.MyViewHolder> {
private List<TopSample> wordslist= Collections.emptyList();
public class MyViewHolder extends RecyclerView.ViewHolder{
public TextView sword;
public MyViewHolder(View view){
super(view);
sword=(TextView)view.findViewById(R.id.vocab);
}
}
public StudentAdapter(List<TopSample> wordslist){
this.wordslist=wordslist;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView=LayoutInflater.from(parent.getContext()).inflate(R.layout.dictionary_list_row,parent,false);
MyViewHolder vh=new MyViewHolder(itemView);
return vh;
//return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
TopSample sample=wordslist.get(position);
holder.sword.setText(sample.getVocab());
}
@Override
public int getItemCount() {
return wordslist.size();
}
}
TopSample代码:
public class TopSample {
public String vocab;
public TopSample(){}
public TopSample(String vocab){
this.vocab=vocab;
}
public String getVocab() {
return vocab;
}
public void setVocab(String vocab) {
this.vocab = vocab;
}
}
content_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_main"
tools:context=".MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical" />
</RelativeLayout>
dictionary_list_row.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:clickable="true"
android:background="?android:attr/selectableItemBackground"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:id="@+id/vocab"
android:textSize="32dp"
android:textStyle="bold"/>
</RelativeLayout>
所以输出就像:
正如您所看到的那样,单个视图持有者按预期使用更长的矩形大小,并且回收器视图未采用完整的match_parent命令。
我希望有一个这样的输出,就像一个例子。:
我尝试在两个xml文件中使用不同的组合更改长度和宽度,但没有按预期获得结果。请看看,告诉我我错在哪里。
还有一件事要补充,我从外部数据库中提取数据,其中我很成功,但我认为这个问题可能是布局问题。请指正。
答案 0 :(得分:0)
问题是你给了TextView
固定的高度和match_parent
宽度,并指定了非常大的字体大小..即32dp。
使用以下代码。您可以删除android:textSize="18sp"
作为默认尺寸。
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/vocab"
android:textSize="18sp"
android:textStyle="bold"
/>
答案 1 :(得分:0)
您是否尝试将RelativeLayout高度设置为wrap_content而不是match_parent?
答案 2 :(得分:0)
在dictionary_list_row.xml
中在相对布局中
android:layout_height="wrap_content"
TextView
android:layout_height="wrap_content"
和
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical" />
答案 3 :(得分:0)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="true"
android:clickable="true"
android:background="?android:attr/selectableItemBackground"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:id="@+id/vocab"
android:textSize="32dp"
android:textStyle="bold"
/>
</RelativeLayout>