所以,我想创建一个ListView片段,但所有我得到的都是空白片段。我的猜测是3个布局资源(R.layout.fragment_home)应该是不同的,但我不能理解哪个。
片段代码:
@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_home:
<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>
尝试返回rootview后,我得到:E / AndroidRuntime(26302):引起:java.lang.RuntimeException:内容具有id属性&#39; android.R.id.list&#39;这不是ListView类 也许这里有问题?
frag = new HomeFragment();
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, frag).commit();
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
setTitle(navMenuTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
数组适配器:
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;
}
}
新闻类:
public class News
{
public String title;
public String link;
public String desc;
public String date;
public News(String title, String link, String desc, String date)
{
this.title = title;
this.link = link;
this.desc = desc;
this.date = date;
}
}
答案 0 :(得分:0)
我的猜测是3个布局资源(R.layout.fragment_home)应该是 不同,但我无法理解。
你是部分正确的。您需要为listView设置一个布局,为片段设置另一个布局。 ListView
属于片段布局
为列表视图项目扩充自定义布局。
您需要更改此
listView = (ListView) rootView.findViewById(R.layout.fragment_home);
到
listView = (ListView) rootView.findViewById(R.id.list);
您需要ListView
中包含fragment_home.xml
个ID列表。
在您的适配器getView
中,您需要为自定义布局充气。
编辑:
HomeFragment
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;
}
}
fragment_home.xml
<?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>
News.java
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;
}
}
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;
}
}
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>
答案 1 :(得分:0)
尝试使用此代码
<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>
Java代码应该是这样的:
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;
}