当项目被单击到另一个片段时,从gridview片段移动

时间:2015-12-19 23:11:53

标签: android android-fragments gridview

我已经设法在片段上放置一个网格视图,并希望在单击某个项目时移动到另一个片段。请帮忙。这是我的网格视图的自定义适配器

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
 import android.widget.Toast;

public class CustomAdapter extends BaseAdapter {

   String [] result;
    Context context;
       int [] imageId;
         private static LayoutInflater inflater=null;
          public CustomAdapter(Context context2, String[] prgmNameList,               int[] prgmImages) {
    // TODO Auto-generated constructor stub
    result=prgmNameList;
    context=context2;
    imageId=prgmImages;
     inflater = ( LayoutInflater )context.
             getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}

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

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

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

public class Holder
{
    TextView tv;
    ImageView img;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    Holder holder=new Holder();
    View rowView;

         rowView = inflater.inflate(R.layout.programlist, null);
         holder.tv=(TextView) rowView.findViewById(R.id.textView1);
         holder.img=(ImageView) rowView.findViewById(R.id.imageView1);

     holder.tv.setText(result[position]);
     holder.img.setImageResource(imageId[position]);

     rowView.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Toast.makeText(context, "You Clicked "+result[position], Toast.LENGTH_LONG).show();
        }
    });

    return rowView;
}

}

这是我的网格视图片段

   import java.util.ArrayList;
   import android.content.Context;
   import android.os.Bundle;
    import android.support.v4.app.Fragment;
     import android.view.LayoutInflater;
        import android.view.View;
            import android.view.ViewGroup;
            import android.widget.AdapterView;
              import android.widget.AdapterView.OnItemClickListener;
        import android.widget.GridView;


            public class Appmenu extends Fragment{

                 @Override
             public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
 View v = inflater.inflate(R.layout.menu, container, false);

    GridView gv;
    Context context;    
    ArrayList prgmName;   

   String [] prgmNameList= new String[] {"Let Us C","c++","JAVA","Jsp","Microsoft .Net","Android","PHP","Jquery","JavaScript"};
   int [] prgmImages= new int[] {R.drawable.c,R.drawable.c,R.drawable.c,R.drawable.c,R.drawable.c,R.drawable.c,R.drawable.c,R.drawable.c,R.drawable.c};

   gv=(GridView) v.findViewById(R.id.gv);    

   gv.setAdapter(new CustomAdapter(getContext(), prgmNameList,prgmImages));    

   gv.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
            long arg3) {

    }
}) ;

 return v;

}

我在哪里放置fragmentManager以便我可以替换片段

提前谢谢

2 个答案:

答案 0 :(得分:0)

单击项目时应替换片段。将此代码添加到onItemClick()

getActivity().getSupportFragmentManager().beginTransaction().replace(container_id, your_new_fragment).commit();

答案 1 :(得分:0)

单击该项目时,应替换该片段。将此代码添加到您的onItemClick()

requireActivity().getSupportFragmentManager().beginTransaction().replace(container_id, new YoutFragment()).commit();