我正在尝试开发一个时间线,就像下面的 facebook 一样。
你看,我有一个 ViewPager 可以在“fragment ”(如提要、个人资料、关于等)之间导航用户。
我有“main.xml”,它上面只有 viewpager;
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
我还有 fragmenta.xml、fragmentb.xml、fragmentc.xml。 让我们检查一下 fragmenta.xml,我把它放在里面;
<FrameLayout 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:background="#FFCC00"
tools:context=".FragmentA" >
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true">
</ListView>
</FrameLayout>
并制作了 singlerow.xml 来将这些框创建成这样的时间线;
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#e67e22"
android:layout_margin=" 5dp">
<ImageView
android:layout_marginTop="5dp"
android:layout_marginBottom="0dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/s1" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:layout_gravity="left"
android:textAlignment="gravity"
android:text="16 likes"/>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:layout_gravity="right"
android:textAlignment="gravity"
android:text="43mins ago"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1">
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:layout_gravity="left"
android:textAlignment="gravity"
android:text="Like" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:layout_gravity="right"
android:textAlignment="gravity"
android:text="Share"/>
</LinearLayout>
</LinearLayout>
我让它只使用一个静态框。但是当我尝试运行这个结构时,我在 Debug模式下遇到错误 “找不到来源 编辑源查找路径”
在我尝试像这样将我的适配器和其他关于 listview 的东西放入 fragmentA.java 之后;
import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnCreateContextMenuListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
public class FragmentA extends Fragment {
ListView list;
String[] sLikes;
String[] sTimes;
int[] images={R.drawable.p1, R.drawable.p2,R.drawable.p3};
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_a,container,false);
Resources res = getResources();
sLikes=res.getStringArray(R.array.likes);
sTimes=res.getStringArray(R.array.times);
list = (ListView) findViewById(R.id.listView1);
sAdapter adapter = new sAdapter(this, sLikes, images, sTimes);
list.setAdapter(adapter);
}
class sAdapter extends ArrayAdapter<String>
{
Context context;
int[] images;
String[] likesArray;
String[] timesArray;
sAdapter(Context c,String[] likes, int imgs[], String[] tms)
{
super(c,R.layout.singlerow,R.id.textView2,likes);
this.context=c;
this.images=imgs;
this.likesArray=likes;
this.timesArray=tms;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//converting to java object
LayoutInflater inflater= (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row=inflater.inflate(R.layout.singlerow,parent,false);
ImageView myImage= (ImageView) row.findViewById(R.id.imageView1);
TextView myLikes = (TextView) row.findViewById(R.id.textView);
TextView myTimes = (TextView) row.findViewById(R.id.textView1);
myImage.setImageResource(images[position]);
myLikes.setText(likesArray[position]);
myTimes.setText(timesArray[position]);
return row;
}
}
}
但是在这里,
list = (ListView) findViewById(R.id.listView1);
CapsAdapter adapter = new CapsAdapter(this, capsLikes, images, capsTimes);
findViewById
和带有红色下划线的新 CapsAdapter
..
我在这里错过了什么。我不应该触摸 FragmentA.java
的内部吗?我将创建另一个 java 文件?请帮我解决这个问题。
-我创建了 SingleRow/Bow 结构, -我在其中创建了 fragment/放置适配器等, -我将它们拉入了 mainactivity.java 文件..
谁能帮我解决这个问题?谢谢。
最佳答案
首先你的调试控制台错误,你可以在这个答案的评论中找到解决方案:I get “Source not found” when debugging my Java code in Eclipse .
然后,“红色下划线”,这是因为 Fragment
的构建完全 不像Activity
。您应该阅读这篇文章:Fragments vs Activities .有一个例子可以看到,在你的 Fragment 中,你必须执行以下操作:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// select an layout to inflate
View view = inflater.inflate(R.layout.fragment_a,container,false);
Resources res = getResources();
sLikes=res.getStringArray(R.array.likes);
sTimes=res.getStringArray(R.array.times);
// call the views with this layout
list = (ListView) view.findViewById(R.id.listView1);
// you see, you need to find the view with "view.find..."
sAdapter adapter = new sAdapter(getActivity(), sLikes, images, sTimes);
list.setAdapter(adapter);
// return the view
return view;
}
还有“新的 CapsAdapter 也带有红色下划线”,因为您需要将上下文附加到您的适配器。 This = 是上下文,但不适用于 fragment 。您需要找到以下上下文:
// Get the Context in Activity
MainActivity.this or this
// Get the Context from Fragments
getActivity()
getApplicationContext()
最后,如果有多个 Class
使用相同的适配器创建 ListView
,您可以在其他文件中创建适配器。
希望这对您有所帮助。
更新:
例如,要为适配器创建和设置 ListView ,您可以执行以下操作:
// declare the Adapter
MyAdapter adapter = new MyAdapter(
this, // to attach the adapter to a context, an activity
MyFirstItem, // an item (string...)
MySecondItem, // an other (image...)
MyOtherItem // and another...
); // end of my adapter
在适配器中,您指定它的性质:
// this method says:
// "hello, i'm an adapter and I am made with..."
sAdapter(
Context c, // "..a context.." (an attachment)
String[] MyFirstItem, // "..a first item which is a string array.."
int MySecondItem[], // "..a second, an image.."
String[] MyOtherItem // etc
)
要工作,您应该将适配器“附加”到“它正在构建的位置”=> Activity => Context。并声明上下文,您有以下示例:
NameOfActivity.this
或this
,示例:to quit an activity, you do MainActivity.this.finish(); or this.finish()
getActivity()
,示例:same as above, but this time you do getActivity().finish(); if your are not IN the Activity class.
getApplicationContext()
:to change a little, you want to make a Toast from a fragment, you should do: Toast.makeText(getApplicationContext(), "I'm in a Fragment...", Toast.LENGTH_LONG).show();
希望它更易于理解。
关于java - 使用 ViewPager 在 Fragment 中创建 Listview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20677469/