Kut*_*müş 2 java xml android listview android-fragments
我正在努力开发像facebook这样的时间轴.
你看,我有一个ViewPager来导航用户之间的"片段",如feed,profile,about等.
我有"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>
Run Code Online (Sandbox Code Playgroud)
我还有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>
Run Code Online (Sandbox Code Playgroud)
并使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>
Run Code Online (Sandbox Code Playgroud)
我只使用一个静态盒子就可以了.但是当我尝试运行这个结构时,我在调试模式上出现错误"Source not found EDIT SOURCE LOOKUP PATH"
在我尝试将关于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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
但在这里,
list = (ListView) findViewById(R.id.listView1);
CapsAdapter adapter = new CapsAdapter(this, capsLikes, images, capsTimes);
Run Code Online (Sandbox Code Playgroud)
findViewById
CapsAdapter
用红色加下划线的新...
我在这里想念的是什么 我不应该触摸内心FragmentA.java
?我会创建另一个java文档?请帮我解决这个问题.
- 我创建了SingleRow/Bow结构,我在其中创建了Fragments/putted adapter等,我将它们拉入mainactivity.java文件中.
任何人都可以帮我这个吗?谢谢.
首先是你的调试控制台错误,你可以在这个答案的评论中找到一个解决方案:在Eclipse中调试我的Java代码时,我得到"Source not found".
然后,"红色下划线",这是因为Fragment
s不是完全像Activity
.您应该阅读本文:碎片与活动.有一个例子可以看到在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;
}
Run Code Online (Sandbox Code Playgroud)
此外,"新的CapsAdapter也强调为红色",因为您需要将Context附加到适配器.这是一个上下文,但它不适用于片段.您需要找到以下内容:
// Get the Context in Activity
MainActivity.this or this
// Get the Context from Fragments
getActivity()
getApplicationContext()
Run Code Online (Sandbox Code Playgroud)
最后,您可以在其他文件Class
中创建适配器,如果有多个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
Run Code Online (Sandbox Code Playgroud)
在适配器中,您可以指定它的性质:
// 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
)
Run Code Online (Sandbox Code Playgroud)
要工作,你应该将适配器"附加"到"它正在构建的地方"=> Activity => Context.要声明上下文,您有以下示例:
NameOfActivity.this
或者this
,例如:
要退出活动,你做MainActivity.this.finish(); 或者this.finish()
getActivity()
例如:
与上面相同,但这次你做getActivity().finish(); 如果你不是在 Activity类中.
getApplicationContext()
:
要改变一点,你想从一个片段做一个Toast,你应该这样做:Toast.makeText(getApplicationContext(),"我在片段......",Toast.LENGTH_LONG).show();
希望它更容易理解.
归档时间: |
|
查看次数: |
8251 次 |
最近记录: |