我刚开始使用Android,但遇到了问题。我有Recycler View
的训练列表,在此视图中,我想制作无论滚动如何都位于静态位置的标题。但是,在recycler view
上方的线性布局中,标题或文本视图不可见。
我不知道如何解决我的问题,有人有任何解决方案或想法吗?
下面,我介绍代码的各个元素。
我的回收者视图:
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/training_recycler"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
我的示例线性布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:context=".TrainingFragment">
<TextView
android:id="@+id/anything"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="40sp"
android:text="@string/monia"/>
</LinearLayout>
在RecyclerView内部,我具有卡片视图,看起来像:
<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="50dp"
android:layout_margin="15dp"
card_view:cardCornerRadius="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:id="@+id/info_text"
android:layout_marginLeft="15dp"
android:layout_marginBottom="15dp"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:textSize="20sp"/>
</LinearLayout>
</android.support.v7.widget.CardView>
当我看设计窗口时,我看到带有文本的textView,但是当我打开模拟器时,TextView没有显示。
谢谢您的建议。
编辑:
TrainingMaterialFragment.java
public class TrainingMaterialFragment extends Fragment {
RecyclerView trainingRecycler;
public TrainingMaterialFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
RecyclerView trainingRecycler = (RecyclerView)inflater.inflate(
R.layout.fragment_training_material, container, false);
int userNo = 1;
String[] nameArray = new String[0];
int[] idArray= new int[0];
try {
SQLiteOpenHelper myFitnessAppDatabaseHelper = new MyFitnessAppDatabaseHelper(inflater.getContext());
View view = inflater.inflate(R.layout.fragment_training, container, false);
SQLiteDatabase db = myFitnessAppDatabaseHelper.getReadableDatabase();
Cursor cursor = db.query("TRAINING_DIARY",
new String[]{"TRAINING_NAME", "DATE",
"DISTANCE","_id"},
"USER = ?",
new String[]{Integer.toString(userNo)},
null, null, null);
int count = cursor.getCount();
nameArray = new String[count];
idArray=new int[count];
if (cursor.moveToFirst()) {
String nameText = cursor.getString(0);
TextView name = (TextView) view.findViewById(R.id.training_name);
name.setText(nameText);
nameArray[0] = nameText;
idArray[0]=cursor.getInt(3);
int i = 1;
while(i < count) {
cursor.moveToNext();
nameText = cursor.getString(0);
nameArray[i] = nameText;
idArray[i]=cursor.getInt(3);
i++;
}
}
cursor.close();
db.close();
} catch (SQLiteException e) {
Toast toast = Toast.makeText(inflater.getContext(), "Baza danych jest niedostępna", Toast.LENGTH_SHORT);
toast.show();
}
TrainingAdapter adapter =
new TrainingAdapter(nameArray);
trainingRecycler.setAdapter(adapter);
LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
trainingRecycler.setLayoutManager(layoutManager);
adapter.setListener(new TrainingAdapter.Listener() {
public void onClick(int position) {
Intent intent = new Intent(getActivity(), TrainingDetailActivity.class);
intent.putExtra(TrainingDetailActivity.EXTRA_TRAINING, position);
getActivity().startActivity(intent);
}
});
return trainingRecycler;
}
}
适配器
public class TrainingAdapter extends RecyclerView.Adapter<TrainingAdapter.ViewHolder> {
private String[] captions;
private Listener listener;
public TrainingAdapter(String[] captions){
this.captions = captions;
}
public static class ViewHolder extends RecyclerView.ViewHolder {
private CardView cardView;
public ViewHolder(CardView v) {
super(v);
cardView = v;
}
}
@Override
public TrainingAdapter.ViewHolder onCreateViewHolder(
ViewGroup parent, int viewType){
CardView cv = (CardView) LayoutInflater.from(parent.getContext())
.inflate(R.layout.training_card_view, parent, false);
return new ViewHolder(cv);
}
@Override
public void onBindViewHolder(ViewHolder holder, final int position){
CardView cardView = holder.cardView;
TextView textView = (TextView)cardView.findViewById(R.id.info_text);
textView.setText(captions[position]);
cardView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (listener != null) {
listener.onClick(position);
}
}
});
}
@Override
public int getItemCount(){
return captions.length;
}
public static interface Listener {
public void onClick(int position);
}
public void setListener(Listener listener) {
this.listener = listener;
}
}
答案 0 :(得分:1)
尝试这种方式。
正如您所说的
LinearLayout in fragment_training.xml
,所以将recylerview放在linearlayout内。
您的fragment_training.xml代码如下所示
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:context=".TrainingFragment">
<TextView
android:id="@+id/anything"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="40sp"
android:text="@string/monia"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/training_recycler"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout/>
然后最终在Java类中进行了更改。
更改此行
RecyclerView trainingRecycler = (RecyclerView)inflater.inflate(
R.layout.fragment_training_material, container, false);
收件人
View rootview = inflater.inflate(
R.layout.fragment_training, container, false);
.....
.....
return rootview;
然后您可以使用rootview找到recylerview。
trainingRecycler = rootview.findViewById(R.id.training_recycler);
并确保已添加返回rootview。.
您的OnCreateView
方法如下所示
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootview = inflater.inflate(
R.layout.fragment_training, container, false);
trainingRecycler = rootview.findViewById(R.id.findViewById);
.....
...
..
return rootview;
答案 1 :(得分:0)
如果我错了,请纠正我,但是您没有将Recycler放在LinearLayout中。您的LinearLayout应该看起来像这样,然后您将能够得到想要的东西:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:context=".TrainingFragment">
<TextView
android:id="@+id/anything"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="40sp"
android:text="@string/monia"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/training_recycler"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout/>
答案 2 :(得分:0)
我在这个项目中做了同样的事情。 这是指向XML文件的链接 https://github.com/ShowYoungg/JournalApp/blob/master/app/src/main/res/layout/activity_main.xml。以及指向Java文件的链接; https://github.com/ShowYoungg/JournalApp/blob/master/app/src/main/java/com/example/android/journalapp/MainActivity.java。 您可以下载整个应用程序并运行,看看我不仅在TcyclView上放了TexView,还放了按钮和其他按钮