Android ListFragment中资源之间的差异
所以,我想创建一个ListView片段,但我得到的只是一个空白片段。我猜3个布局资源(R.layout.fragment_home)应该是不同的,但我不知道是哪个 片段代码:Android ListFragment中资源之间的差异,android,android-layout,android-fragments,android-resources,android-listfragment,Android,Android Layout,Android Fragments,Android Resources,Android Listfragment,所以,我想创建一个ListView片段,但我得到的只是一个空白片段。我猜3个布局资源(R.layout.fragment_home)应该是不同的,但我不知道是哪个 片段代码: @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.lay
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
listView = (ListView) rootView.findViewById(R.layout.fragment_home);
News n = new News("Pavadinimas", "http://www.google.lt", "Apibudinimas", "2015");
news.add(n);
mAdapter = new List(getActivity(), R.layout.fragment_home, news);
setListAdapter(mAdapter);
return listView;
}
R.layout.fragment_主页:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".List" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="14dp"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
阵列适配器:
public class List extends ArrayAdapter<News>{
private final Context context;
private final ArrayList<News> news;
public List(Context context, int resource, ArrayList<News> news){
super(context, resource, news);
this.context = context;
this.news=news;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.fragment_home, parent, false);
TextView textView1 = (TextView) rowView.findViewById(R.id.textView1);
textView1.setText(news.get(position).desc);
textView1.setHint(news.get(position).link);
return rowView;
}
}
我猜应该有3个布局资源(R.layout.fragment_home)
不同,但我不明白是哪个
你部分是对的。您需要插入2个布局,一个用于listView,另一个用于片段<代码>列表视图属于片段布局
为listview项展开自定义布局
你需要改变这个
listView = (ListView) rootView.findViewById(R.layout.fragment_home);
到
您需要在fragment\u home.xml
中具有id列表的列表视图
在适配器getView
中,需要为自定义布局充气
编辑:
家庭片段
public class HomeFragment extends Fragment { // note extends fragment
ListView listView;
ArrayList<News> news = new ArrayList<News>();
List mAdapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
listView = (ListView) rootView.findViewById(R.id.listView1);
News n = new News("Pavadinimas", "http://www.google.lt", "Apibudinimas", "2015");
news.add(n);
mAdapter = new List(getActivity(), 0, news);
listView.setAdapter(mAdapter); // note the change setAdapter
return rootView;
}
}
List.java
class List extends ArrayAdapter<News>
{
LayoutInflater mInflater;
ArrayList<News> news;
public List(Context context, int resource, ArrayList<News> news) {
super(context, resource, news);
mInflater = LayoutInflater.from(context);
this.news = news;
}
public View getView(int pos, View arg1, ViewGroup arg2) {
ViewHolder holder;
if(arg1==null)
{
arg1=mInflater.inflate(R.layout.list_item, null);
holder= new ViewHolder();
holder.tv1 = (TextView) arg1.findViewById(R.id.textView1);
holder.tv2 = (TextView) arg1.findViewById(R.id.textView2);
holder.tv3 = (TextView) arg1.findViewById(R.id.textView3);
arg1.setTag(holder);
}else
{
holder = (ViewHolder) arg1.getTag();
}
News n= news.get(pos);
holder.tv1.setText(n.title);
return arg1;
}
static class ViewHolder
{
TextView tv1,tv2,tv3;
}
}
类列表扩展了ArrayAdapter
{
拉平机;
ArrayList新闻;
公共列表(上下文、int资源、ArrayList新闻){
超级(上下文、资源、新闻);
mInflater=LayoutInflater.from(上下文);
this.news=新闻;
}
公共视图getView(内部位置、视图arg1、视图组arg2){
视窗座;
如果(arg1==null)
{
arg1=mInflater.flate(R.layout.list_项,空);
holder=新的ViewHolder();
holder.tv1=(TextView)arg1.findViewById(R.id.textView1);
holder.tv2=(TextView)arg1.findViewById(R.id.textView2);
holder.tv3=(TextView)arg1.findViewById(R.id.textView3);
arg1.设置标签(持有人);
}否则
{
holder=(ViewHolder)arg1.getTag();
}
News n=News.get(pos);
holder.tv1.setText(n.title);
返回arg1;
}
静态类视窗夹
{
文本视图tv1、tv2、tv3;
}
}
list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="31dp"
android:layout_marginTop="53dp"
android:text="TextView" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView1"
android:layout_alignBottom="@+id/textView1"
android:layout_centerHorizontal="true"
android:text="TextView" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView2"
android:layout_alignBottom="@+id/textView2"
android:layout_alignParentRight="true"
android:layout_marginRight="21dp"
android:text="TextView" />
</RelativeLayout>
尝试使用此代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".List" >
<ListView
android:id="@+id/list"
android:layout_height="wrap_content"
android:layout_width="match_parent">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="14dp"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
</ListView>
</LinearLayout>
}返回rootView
。您将fragment\u home
充气,并且还将R.layout.fragment\u home
传递一个参数给适配器类的构造函数。你是否也在adapter getView中膨胀了相同的布局?你需要在片段代码和适配器代码满后提供更多信息。您还有listView=(listView)rootView.findViewById(R.layout.fragment\u home)
。你做错了,我应该用“R.layout.fragment\u home”来增加其他布局吗?我不明白你在说什么where@user3298387这也是错误的,它必须是一个id。这是一个复制粘贴错误确定,所以我更换了,但它仍然是相同的错误。我想我应该把
放在某个地方,因为如果只有一个元素自身扩展,那么就应该放在其他地方。@user3298387您将只看到标题。同时更新othere视图您将看到所需的所有数据,您也可以自定义这些数据。看起来不错,但现在其他错误ocurs由以下原因引起:java.lang.IllegalStateException:指定的子级已具有父级。您必须首先对孩子的家长调用removeView()。
@user3298387您需要更新您的帖子。我猜不出来,张贴stacktrace会有帮助
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</ListView>
</RelativeLayout>
public class News {
String name,url,title,year;
public News(String name, String url, String title, String year) {
// TODO Auto-generated constructor stub
this.name= name;
this.url = url;
this.title = title;
this.year=year;
}
}
class List extends ArrayAdapter<News>
{
LayoutInflater mInflater;
ArrayList<News> news;
public List(Context context, int resource, ArrayList<News> news) {
super(context, resource, news);
mInflater = LayoutInflater.from(context);
this.news = news;
}
public View getView(int pos, View arg1, ViewGroup arg2) {
ViewHolder holder;
if(arg1==null)
{
arg1=mInflater.inflate(R.layout.list_item, null);
holder= new ViewHolder();
holder.tv1 = (TextView) arg1.findViewById(R.id.textView1);
holder.tv2 = (TextView) arg1.findViewById(R.id.textView2);
holder.tv3 = (TextView) arg1.findViewById(R.id.textView3);
arg1.setTag(holder);
}else
{
holder = (ViewHolder) arg1.getTag();
}
News n= news.get(pos);
holder.tv1.setText(n.title);
return arg1;
}
static class ViewHolder
{
TextView tv1,tv2,tv3;
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="31dp"
android:layout_marginTop="53dp"
android:text="TextView" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView1"
android:layout_alignBottom="@+id/textView1"
android:layout_centerHorizontal="true"
android:text="TextView" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView2"
android:layout_alignBottom="@+id/textView2"
android:layout_alignParentRight="true"
android:layout_marginRight="21dp"
android:text="TextView" />
</RelativeLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".List" >
<ListView
android:id="@+id/list"
android:layout_height="wrap_content"
android:layout_width="match_parent">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="14dp"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
</ListView>
</LinearLayout>
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
listView = (ListView) rootView.findViewById(R.id.list);
News n = new News("Pavadinimas", "http://www.google.lt", "Apibudinimas", "2015");
news.add(n);
mAdapter = new List(getActivity(), R.layout.fragment_home, news);
listview.setAdapter(mAdapter);
return listView;