如何使列表视图出现在片段中?
Posted
技术标签:
【中文标题】如何使列表视图出现在片段中?【英文标题】:How do I cause listview to appear in fragment? 【发布时间】:2020-03-25 07:29:39 【问题描述】:我正在尝试获取由数组提供支持的列表视图,并且我正在使用 android Studio 附带的一些示例代码。为什么我的列表视图没有出现在片段中?
我正在使用来自Vogella Android List View 的代码作为列表视图适配器。我不明白我做错了什么导致列表视图不出现。应该很简单。
package com.example.bento.ui.home;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import androidx.annotation.Nullable;
import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;
import com.example.bento.R;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class HomeFragment extends Fragment
private HomeViewModel homeViewModel;
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState)
homeViewModel =
ViewModelProviders.of(this).get(HomeViewModel.class);
View root = inflater.inflate(R.layout.fragment_home, container, false);
final TextView textView = root.findViewById(R.id.text_home);
homeViewModel.getText().observe(this, new Observer<String>()
@Override
public void onChanged(@Nullable String s)
textView.setText(s);
);
final ListView listview = (ListView) root.findViewById(R.id.listview);
String[] values = new String[] "Android", "iPhone", "WindowsMobile",
"Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
"Linux", "OS/2", "Ubuntu", "Windows7", "Max OS X", "Linux",
"OS/2", "Ubuntu", "Windows7", "Max OS X", "Linux", "OS/2",
"Android", "iPhone", "WindowsMobile" ;
final ArrayList<String> list = new ArrayList<String>();
for (int i = 0; i < values.length; ++i)
list.add(values[i]);
final StableArrayAdapter adapter = new StableArrayAdapter(getContext(),
android.R.layout.simple_list_item_1, list);
listview.setAdapter(adapter);
listview.setOnItemClickListener(new AdapterView.OnItemClickListener()
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
@Override
public void onItemClick(AdapterView<?> parent, final View view,
int position, long id)
final String item = (String) parent.getItemAtPosition(position);
view.animate().setDuration(2000).alpha(0)
.withEndAction(new Runnable()
@Override
public void run()
list.remove(item);
adapter.notifyDataSetChanged();
view.setAlpha(1);
);
);
return root;
@Override
public void onCreate(@Nullable Bundle savedInstanceState)
super.onCreate(savedInstanceState);
private class StableArrayAdapter extends ArrayAdapter<String>
HashMap<String, Integer> mIdMap = new HashMap<String, Integer>();
public StableArrayAdapter(Context context, int textViewResourceId,
List<String> objects)
super(context, textViewResourceId, objects);
for (int i = 0; i < objects.size(); ++i)
mIdMap.put(objects.get(i), i);
@Override
public long getItemId(int position)
String item = getItem(position);
return mIdMap.get(item);
@Override
public boolean hasStableIds()
return true;
这是我的布局
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_
android:layout_>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/listview"
android:layout_
android:layout_
app:layout_constraintBottom_toBottomOf="parent"/>
<TextView
android:id="@+id/text_home"
android:layout_
android:layout_
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:textAlignment="center"
android:textSize="20sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
在我的主页片段中,没有出现列表视图。
【问题讨论】:
在日志中出现任何错误? fragment/adapter/xml/listview没有问题——可以查看viewmodel部分 【参考方案1】:将布局中的 ListView 标记更改为:
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/listview"
android:layout_
android:layout_
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/text_home"/>
通过将宽度和高度设置为 0dp
并提供所有四个约束,您将完全定义 ListView 应有的大小。
【讨论】:
【参考方案2】:我无法弄清楚这里出了什么问题,但我也不知道为什么在有 Recycler 视图时使用 ListView。我会为你发布一个示例回收器查看代码。
MainFragment.java:
public class MainFragment.java extends Fragment
private RecyclerView recyclerView;
private CustomAdapter adapter;
private List<YourObject> objectsList;
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
View view = inflater.inflate(R.layout.fragment_layout, container, false);
recyclerView = view.findViewById(R.id.recycler_view);
//add dummy data
objectsList = new ArrayList<>();
objectsList.add(new YourObject());
objectsList.add(new YourObject());
objectsList.add(new YourObject());
objectsList.add(new YourObject());
adapter = new CustomAdapter(this, objectsList);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.setAdapter(adapter);
return view;
CustomAdapter.java:
public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.ViewHolder>
private Context context;
private List<YourObject> objectsList;
public TransactionListAdapter(Context context, List<Transaction> transactionList)
this.context = context;
this.transactionsList = transactionList;
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_item_layout, parent, false);
return new ViewHolder(view);
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position)
final YourObject object = objectsList.get(position);
holder.textView.setText("Stuff here");
holder.imageView.setImageResource(R.id.myNiceImage);
class ViewHolder extends RecyclerView.ViewHolder
private ImageView imageView;
private TextView textView;
ViewHolder(@NonNull View view)
super(view);
imageView.findViewById(R.id.image_view);
textView.findViewById(R.id.text_view);
recycler_item_layout.xml:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="hotizontal"
android:layout_
android:layout_>
<ImageView
android:id="@+id/image_view"
android:layout_
android:layout_/>
<TextView
android:id="@+id/image_view"
android:layout_
android:layout_/>
</LinearLayout>
fragment_layout.xml:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="hotizontal"
android:layout_
android:layout_>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_
android:layout_/>
</LinearLayout>
这比 ListView 好很多,而且易于实现
【讨论】:
以上是关于如何使列表视图出现在片段中?的主要内容,如果未能解决你的问题,请参考以下文章