Android:GridView项目单击转到一个片段到另一个片段

时间:2015-07-28 06:29:31

标签: android gridview fragment

我底部有一个标签栏。它有四个标签。这是选项卡的代码。当我点击gridview项目时,我想转到另一个片段但是标签保持相同,只有片段更改。代码没有错误,但是当我点击gridview项目时它不起作用。 这是我的FeedFragment.java

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;

public class FeedFragment extends Fragment {
    GridView grid;
String[] web= new String[]{"Free drinks for ladies in the bar",
        "Saturday Night live at fork you",
        "Free drinks for ladies in the bar",
        "Free drinks for ladies in the bar",
        "Free drinks for ladies in the bar",
        "Free drinks for ladies in the bar",
        "Free drinks for ladies in bar",
        "Free drinks for ladies",
        "Free drinks for ladies",
        "Free drinks for ladies",
        "Free drinks for ladies",
        "Free drinks for ladies",
        "Free drinks for ladies",

        "Blogger"};
        int[] imageid={R.drawable.newfeed,
                R.drawable.newfeed,
                R.drawable.newfeed,
                R.drawable.newfeed,
                R.drawable.newfeed,
                R.drawable.newfeed,
                R.drawable.newfeed,
                R.drawable.newfeed,
                R.drawable.newfeed,
                R.drawable.newfeed,
                R.drawable.newfeed};
String[] name={"Natasha Sethi" ,
        "Natasha Sethi" ,
        "Natasha Sethi" ,
        "Natasha Sethi" ,
        "Natasha Sethi" ,
        "Natasha Sethi" ,
        "Natasha Sethi" ,
        "Natasha Sethi" ,
        "Natasha Sethi" ,
        "Natasha Sethi" ,"Natasha Sethi" ,"Natasha Sethi" ,
        "Natasha Sethi" ,
        "Blogger" };
    String[] time={
             "5m ago" ,
            "5m ago" ,
            "5m ago" ,
            "5m ago" ,
            "5m ago" ,
            "5m ago" ,
            "5m ago" ,"5m ago" ,
            "5m ago" ,
            "5m ago" ,
            "5m ago" ,
            "5m ago" ,
            "5m ago" ,

            "Blogger"
    };
    int[] imagelike={
            R.drawable.like,
            R.drawable.like,
            R.drawable.like,
            R.drawable.like,
            R.drawable.like,
            R.drawable.like,
            R.drawable.like,
            R.drawable.like,
            R.drawable.like,
            R.drawable.like,
            R.drawable.like
    };
    int[] personimg={
            R.drawable.white,
            R.drawable.white, R.drawable.white, R.drawable.white, R.drawable.white, R.drawable.white, R.drawable.white, R.drawable.white, R.drawable.white, R.drawable.white, R.drawable.white, R.drawable.white, R.drawable.white, R.drawable.white, R.drawable.white, R.drawable.white, R.drawable.white, R.drawable.white
    };

Intent intent;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View view= inflater.inflate(R.layout.feedmain, container, false);
        FeedCustomGrid adapter= new FeedCustomGrid(getActivity().getApplicationContext(),web,imageid,name,time,imagelike,personimg);


        grid=(GridView)view.findViewById(R.id.grid);

        grid.setAdapter(adapter);
        grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {


            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Fragment fragment = new Feed2();

                    FragmentManager fragmentManager = getFragmentManager();
                    fragmentManager.beginTransaction().add(R.id.container_framelayout,fragment).addToBackStack(null).commit();

        }});





    return view;}
            @Override
            public void onActivityCreated(Bundle savedInstanceState) {
                // TODO Auto-generated method stub
                super.onActivityCreated(savedInstanceState);



}}

这是FeedCustomGrid.Java

public class FeedCustomGrid extends BaseAdapter {
    private Context mContext;
    private final String[] web;
    private final String[] name;
    private final String[] time;
    private final int[] Imageid;
    private final int[] imagelike;
    private final int[] personimg;

    public FeedCustomGrid(Context c, String[] web,int[] Imageid,String[] name,String[] time,int[] imagelike,int[] personimg) {

        mContext = c;
        this.Imageid = Imageid;
        this.web = web;
        this.name=name;
        this.time=time;
        this.imagelike=imagelike;
        this.personimg=personimg;
    }



    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return web.length;

    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        View grid;
        LayoutInflater inflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        if (convertView == null) {

            grid = new View(mContext);
            grid = inflater.inflate(R.layout.feedmainitem, null);
            TextView textView = (TextView) grid.findViewById(R.id.textView);
            TextView textView26 = (TextView) grid.findViewById(R.id.textView26);
            TextView textView29 = (TextView) grid.findViewById(R.id.textView29);
            ImageView imageView = (ImageView)grid.findViewById(R.id.imageView8);
            ImageView imageView1 = (ImageView)grid.findViewById(R.id.imageView17);
            ImageButton imageView2 = (ImageButton)grid.findViewById(R.id.imageButton12);
            textView.setText(web[position]);
            textView29.setText(time[position]);
            textView26.setText(name[position]);
            imageView.setImageResource(Imageid[position]);
            imageView2.setImageResource(imagelike[position]);
            imageView1.setImageResource(personimg[position]);
        } else {
            grid = (View) convertView;
        }

        return grid;
    }
}

0 个答案:

没有答案