简体   繁体   中英

Creating Listview in Fragment with ViewPager

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 Fragment s 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.

Do the following:

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

You should fill in your listView on the onActivityCreated to avoid NullPointerErrors .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM