【问题标题】:Creating Listview in Fragment with ViewPager使用 ViewPager 在 Fragment 中创建 Listview
【发布时间】:2014-01-07 18:31:40
【问题描述】:

我正在尝试开发一个时间线,如下面的 facebooks 之一。

你看,我有一个 ViewPager 可以在“片段”之间导航用户,如提要、个人资料、关于等。

我有“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>

我让它只使用一个静态框。但是当我尝试运行这个结构时,我在调试模式下遇到错误 “找不到来源 编辑源查找路径"

在我尝试像这样将我的适配器和其他关于 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 结构, -我在其中创建了片段/放置适配器等, -我把它们拉到 mainactivity.java 文档中..

谁能帮我解决这个问题?谢谢。

【问题讨论】:

    标签: java android xml listview android-fragments


    【解决方案1】:

    首先你的调试控制台错误,你可以在这个答案的 cmets 中找到解决方案:I get “Source not found” when debugging my Java code in Eclipse
    然后是“红色下划线”,这是因为 Fragments 不像 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 也带有红色下划线”,因为您需要将 Context 附加到您的适配器。 This = 是一个上下文,但它不适用于片段。你需要找到上下文是:

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

    最后,如果有多个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  
    

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

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

    要工作,您应该将适配器“附加”到“它正在构建的位置”=> 活动 => 上下文。为了声明上下文,您有以下示例:

    1. NameOfActivity.thisthis,例如:

      要退出活动,请执行 MainActivity.this.finish();this.finish()

    2. getActivity(),例如:

      与上面相同,但如果您不是 IN Activity 类,这次您将执行 getActivity().finish();

    3. getApplicationContext()

      稍微改变一下,你想从一个片段制作一个 Toast,你应该这样做: Toast.makeText(getApplicationContext(), "I'm in a Fragment...", Toast .LENGTH_LONG).show();

    希望它更容易理解。

    【讨论】:

    • 首先,感谢您的回答伙伴。我应用了这个,但是“getApplicationContext()”方法里面有什么?您在上面看到了我的代码,要在里面移动什么?谢谢。需要你的帮助
    • "里面要搬什么?" - 如果你是3个片段,在这种情况下class sAdapter extends ArrayAdapter,你可以使用这个类sAdapter并创建一个新文件来放置这个代码。之后,你只需要导入( import com.myapp.test.sAdapter;) 这个类适配器在你的 Fragments 中。
    • 我现在明白了,谢谢伙计。但是当我到达滚动的底部(仍然有1-2张图片)时,程序“不幸停止”正在发生。为什么会这样,有什么想法吗?在 logchat 中,红线是这样开始的; -FATAL EXCEPTION:main, -Process etc... -java.lang.OutofMemoryError 并继续......
    • 检查您的 Arrays 数字。这是一条线索:stackoverflow.com/a/17047194/2668136 |位置从0开始,别忘了。
    • 不客气。不,抱歉,通过这些修改,您没有从片段中触摸/自定义您的操作选项卡。您应该检查您的 style.xml 并使用所有版本对其进行自定义,请参阅:developer.android.com/guide/topics/ui/actionbar.html#Styleandroid-developers.blogspot.fr/2011/04/…。它可能会帮助您在所有设备上使用相同的选项卡。
    【解决方案2】:

    执行以下操作:

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        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);
    
        list = (ListView) findViewById(R.id.listView1);
    return view;
    }    
    
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);
        sAdapter adapter = new sAdapter(this, sLikes, images, sTimes);
            list.setAdapter(adapter);
    }
    

    您应该在onActivityCreated 上填写您的listView 以避免NullPointerErrors

    【讨论】:

    • 朋友,你能说得具体点吗? “public View onCreateView”和“public void onActivityCreated”里面会有什么?
    • 我应用了这个结构 mate 但是“new sAdapter(this, sLikes, images, sTimes);”下划线..它说;构造函数 FragmentA.CapsAdapter(FragmentA, String[], int[], String[]) 未定义。请帮我解决这个问题。
    • 声明新的sAdapter(getActivity(), sLikes, images, sTimes);
    • @KuthayGümüş 没问题 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-05
    • 2015-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多