使用onclick创建列表视图,该列表视图转到片段

时间:2015-05-20 20:09:59

标签: android baseadapter android-actionbaractivity

您想创建一个列表视图,当您单击它时,它将转到片段。我尝试创建一个扩展到BaseAdapter的CustomAdapter,但我遇到了fragment和getSupportFragmentManager()的问题,它说"无法解析方法" getSupportFragmentManager()"

这是我的CustomAdapter的代码

public class CustomAdapter extends BaseAdapter {
Activity activity;
String [] result;
Context context;
int [] imageId;
private static LayoutInflater inflater=null;

public CustomAdapter(MainActivity mainActivity, int[] prgmImages) {
    context=mainActivity;
    imageId=prgmImages;
    inflater = ( LayoutInflater )context.
            getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
    return imageId.length;
}

@Override
public Object getItem(int position) {
    return position;
}

@Override
public long getItemId(int position) {
    return position;
}

public class Holder
{
    ImageView img;
}


@Override
public View getView(final int position, View convertView, ViewGroup parent) {

    Holder holder=new Holder();
    View rowView;
    rowView = inflater.inflate(R.layout.menu_list, null);
    holder.img=(ImageView) rowView.findViewById(R.id.imageView1);
    holder.img.setImageResource(imageId[position]);
    rowView.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {

            Fragment frag = new Fragment();

            if (position == 0) {

            } else if (position == 1) {
                frag = new ServicesFrag();
            } else if (position == 2) {

            } else if (position == 3) {

            } else if (position == 4) {

            }

            if (frag != null) {
                FragmentManager fragmentManager = getSupportFragmentManager();
                fragmentManager.beginTransaction()
                        .replace(R.id.frame_container, frag).addToBackStack(null).commit();


            } else {
                // error in creating fragment
                Log.e("MainActivity", "Error in creating fragment");
            }
            // TODO Auto-generated method stub
            Toast.makeText(context, "You Clicked " + position, Toast.LENGTH_LONG).show();
        }
    });
    return rowView;
}

}

谢谢你的帮助。

1 个答案:

答案 0 :(得分:0)

试试这个,

ListView listView = (ListView)findViewById(R.id.listView);
CustomAdapter adapter = new CustomAdapter(activity, array);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapter, View view, int position, long arg)   {

    Fragment frag = new Fragment();

        if (position == 0) {

        } else if (position == 1) {
            frag = new ServicesFrag();
        } else if (position == 2) {

        } else if (position == 3) {

        } else if (position == 4) {

        }

        if (frag != null) {
            FragmentManager fragmentManager = getSupportFragmentManager();
            fragmentManager.beginTransaction()
                    .replace(R.id.frame_container, frag).addToBackStack(null).commit();


    }
});