与ViewPager创建列表视图中的片段 [英] Creating Listview in Fragment with ViewPager

查看:256
本文介绍了与ViewPager创建列表视图中的片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图开发类似Facebook的一个时间线下面。

您看,我有一个ViewPager之间碎片浏览用户像饲料,轮廓,约等。

我有main.xml中它只有viewpager就这样说;

 < android.support.v4.view.ViewPager
    的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:ID =@ + ID /寻呼机
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent>
< /android.support.v4.view.ViewPager>
 

和我也有fragmenta.xml,fragmentb.xml,fragmentc.xml。 让我们检查的fragmenta.xml,在它的内部,我把;

 <的FrameLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:后台=#FFCC00
    工具:上下文=碎裂。>

    <的ListView
        机器人:ID =@ + ID / ListView1的
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentTop =真
        机器人:layout_alignParentLeft =真正的>
    < / ListView控件>

< /的FrameLayout>
 

和制作singlerow.xml创建盒装入一个时间表是这样;

 < XML版本=1.0编码=UTF-8&GT?;
<的LinearLayout
        的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:方向=垂直
        机器人:后台=#e67e22
        机器人:layout_margin =5DP>

        < ImageView的
            机器人:layout_marginTop =5DP
            机器人:layout_marginBottom =0dp
            机器人:layout_marginLeft =5DP
            机器人:layout_marginRight =5DP
            机器人:ID =@ + ID / imageView1
            机器人:layout_width =match_parent
            机器人:layout_height =WRAP_CONTENT
            机器人:SRC =@可绘制/ S1/>

        <的LinearLayout
            机器人:layout_width =FILL_PARENT
            机器人:layout_height =WRAP_CONTENT
            机器人:方向=横向
            机器人:weightSum =1>

            <的TextView
                机器人:ID =@ + ID / TextView的
                机器人:layout_width =WRAP_CONTENT
                机器人:layout_height =WRAP_CONTENT
                机器人:layout_weight =0.5
                机器人:layout_gravity =左
                机器人:textAlignment =万有引力
                机器人:文本=16喜欢/>

            <的TextView
                机器人:ID =@ + ID / textView1
                机器人:layout_width =WRAP_CONTENT
                机器人:layout_height =WRAP_CONTENT
                机器人:layout_weight =0.5
                机器人:layout_gravity =右
                机器人:textAlignment =万有引力
                机器人:文本=43mins前/>

        < / LinearLayout中>

        <的LinearLayout
            机器人:layout_width =FILL_PARENT
            机器人:layout_height =WRAP_CONTENT
            机器人:方向=横向
            机器人:weightSum =1>

            <按钮
                机器人:ID =@ + ID /按钮1
                机器人:layout_width =WRAP_CONTENT
                机器人:layout_height =WRAP_CONTENT
                机器人:layout_weight =0.5
                机器人:layout_gravity =左
                机器人:textAlignment =万有引力
                机器人:文本=像/>

            <按钮
                机器人:ID =@ + ID /按钮2
                机器人:layout_width =WRAP_CONTENT
                机器人:layout_height =WRAP_CONTENT
                机器人:layout_weight =0.5
                机器人:layout_gravity =右
                机器人:textAlignment =万有引力
                机器人:文本=分享/>

        < / LinearLayout中>
< / LinearLayout中>
 

我把它只有一个静态框中工作。但是,当我尝试运行这个结构,我得到的调试模式错误 源未找到 编辑源查找跳

在我试图把我的适配器和其他东西有关的ListView到fragmentA.java这样;

 进口android.content.Context;
进口android.content.res.Resources;
进口android.os.Bundle;
进口android.support.v4.app.Fragment;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.View.OnCreateContextMenuListener;
进口android.view.ViewGroup;
进口android.widget.ArrayAdapter;
进口android.widget.ImageView;
进口android.widget.ListView;
进口android.widget.TextView;

公共类碎裂扩展片段{

    ListView控件列表;
    的String [] sLikes;
    的String [] sTimes;
    INT []的图像= {R.drawable.p1,R.drawable.p2,R.drawable.p3};

    @覆盖
    公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,捆绑savedInstanceState){
        返回inflater.inflate(R.layout.fragment_a,集装箱,假);

        资源RES = getResources();
        sLikes = res.getStringArray(R.array.likes);
        sTimes = res.getStringArray(R.array.times);

        名单=(ListView控件)findViewById(R.id.listView1);
        sAdapter适配器=新sAdapter(这一点,sLikes,图像,sTimes);
        list.setAdapter(适配器);
    }


    类sAdapter扩展ArrayAdapter<字符串>
    {
        上下文语境;
        INT []的图像;
        的String [] likesArray;
        的String [] timesArray;
        sAdapter(上下文C,的String []喜欢,INT IMGS []的String [] TMS)
        {
            超级(C,R.layout.singlerow,R.id.textView2,喜欢);
            this.context = C;
            this.images = IMGS;
            this.likesArray =喜欢;
            this.timesArray = TMS;
        }

        @覆盖
        公共查看getView(INT位置,查看convertView,ViewGroup中父){
            //转换为Java对象
            LayoutInflater充气=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            查看排= inflater.inflate(R.layout.singlerow,父母,假);

            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(图片[位置]);
            myLikes.setText(likesArray [位置]);
            myTimes.setText(timesArray [位置]);

            返回行;
        }
    }
}
 

但在这里,

 名单=(ListView控件)findViewById(R.id.listView1);
CapsAdapter适配器=新CapsAdapter(这一点,capsLikes,图像,capsTimes);
 

findViewById 及新 CapsAdapter 中强调了与红色。

我缺少的是在这里。我不应该接触的内 FragmentA.java ?我将创造另一个java文件?请帮我这件事。

-I创建SingleRow /弓结构, -I创建片段/推适配器等在它的内部, -I拉他们到mainactivity.java文件。

谁能帮我这个?谢谢你。

解决方案

首先,你的调试控制台错误,你可以找到这个答案在评论一个解决方案:的我得到找不到源代码调试在Eclipse 我的Java code时。
于是,红色下划线,那是因为片段,则无法建立完全活动。你应该看看这篇文章:片段VS活动 。有一个例子,看看在你的碎片,你必须做到以下几点:

  @覆盖
公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,捆绑savedInstanceState){
    //选择一个布局膨胀
    查看查看= inflater.inflate(R.layout.fragment_a,集装箱,假);

    资源RES = getResources();
    sLikes = res.getStringArray(R.array.likes);
    sTimes = res.getStringArray(R.array.times);

    //调用这个布局的意见
    名单=(ListView控件)view.findViewById(R.id.listView1);
    //你看,你需要找到这样的观点,view.find ......

    sAdapter适配器=新sAdapter(getActivity(),sLikes,图像,sTimes);
    list.setAdapter(适配器);

    //返回视图
    返回查看;
}
 

此外新CapsAdapter是强调红太,因为你需要一个上下文连接到您的适配器。 =是一个方面,但它并不适用于一个片段。你需要找到的语境是:

  //获取活动的背景下
MainActivity.this或本
//从片段的上下文
getActivity()
getApplicationContext()
 

最后,你可以让你的适配器在其他文件中,如果有几个它创建一个的ListView 与相同的适配器。

希望这有助于。


更新:
例如,制作和设置列表视图到适配器,请执行以下操作:

  //声明适配器
MyAdapter适配器=新MyAdapter(
                         此,//向适配器附加到上下文中,活动
                         MyFirstItem,//项目(字符串...)
                         MySecondItem,//一个其他(图片...)
                         MyOtherItem //而另一...
                    ); //我的适配器端
 

在适配器,您可以指定它的性质:

  //这个方法说:
//你好,我是一个适配器,我有做...
sAdapter(
      上下文C,//..A环境..(附件)
      的String [] MyFirstItem,//,这是一个字符串数组..A第一个项目。
      INT MySecondItem [],//..A第二,图像......
      的String [] MyOtherItem //等
)
 

要工作,你应该重视适配器,其中它的建设=>活动=>语境。并申报的情况下,你有下面的例子:

  1. NameOfActivity.this ,例如:

      

    要退出一个活动,你做的 MainActivity.this.finish(); this.finish()

  2. getActivity(),例如:

      

    上面一样,但这次你做的 getActivity()完成(); 如果你不是的 Activity类。

  3. getApplicationContext()

      

    要改变一点点,你想从一个片段敬酒,你应该做的:Toast.makeText( getApplicationContext(),我在一个片段......,吐司.LENGTH_LONG).show();

希望这是更容易理解。

I'm trying to develop a timeline like facebooks one on below.

You see, I have a ViewPager to navigate user between "Fragments" like feed, profile, about etc..

I have "main.xml" which has just viewpager on it like this;

<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>

and also I have fragmenta.xml, fragmentb.xml, fragmentc.xml. Lets inspect on fragmenta.xml, inside of it, I put;

<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>

And made singlerow.xml to create that boxes into a timeline like this;

<?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>

I made it worked with just one static box. But when I try to run this structure I get an error on debug mode "Source not found EDIT SOURCE LOOKUP PATH"

After I tried to put my adapter and other stuffs about listview into fragmentA.java like this;

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;
        }
    }
}

But in here,

list = (ListView) findViewById(R.id.listView1);
CapsAdapter adapter = new CapsAdapter(this, capsLikes, images, capsTimes);

findViewById and new CapsAdapter in underlined with red..

What am I missing here. I should not touch inside of FragmentA.java ? I will create another java document ? Please help me about this.

-I created SingleRow/Bow structure, -I created Fragments / putted adapter etc inside of it, -I pulled them into mainactivity.java document..

Can anyone help me about this ? Thank you.

解决方案

First of all your Debug Console Error, you can find a solution in the comments on this answer: I get "Source not found" when debugging my Java code in Eclipse.
Then, the "red underline", it is because Fragments are not build exactly like Activity. You should read this article: Fragments vs Activities. There is an example to see that in your Fragment, you must to do the following:

 @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;
}    

Also "new CapsAdapter is underline red too", because you need to attach a Context to your adapter. This = is a context, but it's not applicable for a fragment. You need to find the Context which is:

// Get the Context in Activity
MainActivity.this or this  
// Get the Context from Fragments 
getActivity()  
getApplicationContext()  

Finally, you can make your adapter in other file, if there are several Class which create a ListView with the same adapter.

Hope this helps.


UPDATE:
For example, to make and set a listview to an adapter, you do the following:

// 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  

In the adapter, you specify the nature of it:

// 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
)  

To works, you should "attach" the adapter to "where it's building" => the Activity => the Context. And to declare the context, you have the following example:

  1. NameOfActivity.this or this, example:

    to quit an activity, you do MainActivity.this.finish(); or this.finish()

  2. getActivity(), example:

    same as above, but this time you do getActivity().finish(); if your are not IN the Activity class.

  3. 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();

Hope it's more understandable.

这篇关于与ViewPager创建列表视图中的片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆