3

我想在 Grid 和 List 项目的项目点击上添加和替换 Fragment-1 上的 Fragment 和 Fragment-2 上的 Frame 布局。

我创建了 main.xml 类

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >

<LinearLayout
    android:layout_width="0dp"
    android:layout_weight="1"
    android:orientation="vertical"
    android:layout_height="fill_parent" >

    <fragment
        android:id="@+id/fragment1"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        class="com.example.fragment.Fragment1" />

    <View
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:background="#000000" />

    <fragment
        android:id="@+id/fragment2"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        class="com.example.fragment.Fragment2" />
</LinearLayout>

<View
    android:layout_width="1dp"
    android:layout_height="fill_parent"
    android:background="#000000" />

<FrameLayout
    android:id="@+id/fragment3"
    android:layout_width="0dp"
    android:layout_height="fill_parent"
    android:layout_weight="2"/>

这是我的 fragment_1.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="8dp"
android:paddingRight="8dp" >

<GridView
    android:id="@+id/Grid1"
    android:layout_width="match_parent"
    android:numColumns="auto_fit"
    android:columnWidth="100dp"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:drawSelectorOnTop="false" />

这是我的 row_fragment1_list.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="fill_parent"
android:layout_margin="15dp"
android:orientation="vertical" >

<ImageView
    android:id="@+id/img_view"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:background="#CCCCCC"
    android:textSize="16sp"
    android:textStyle="bold" />

<TextView
    android:id="@+id/text2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="16sp" />

和 fragment_3.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >

<TextView
    android:id="@+id/tv_fragment3"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textSize="22sp"
    android:gravity="center"
    android:text="This is 3rd Fragment" />

这是我的 Main.xml

public class Main extends Activity
{
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

这是 Fragment1.class

public class Fragment1 extends Fragment implements OnItemClickListener
{
    Activity myActivity;
    GridView mGridView; 
    private String ListItem[] = {"Item 1","Item 2","Item 3","Item 4","Item 5","Item 6","Item 7"};
    private int imgID[] = {R.drawable.admin_access_rule,
            R.drawable.admin_backup,R.drawable.admin_browsesite,
            R.drawable.admin_comment_post,R.drawable.admin_content,
            R.drawable.admin_content_type,R.drawable.admin_logout,};

    @Override
    public void onAttach(Activity activity) 
    {
        super.onAttach(activity);
        this.myActivity = activity;
        Toast.makeText(activity.getApplicationContext(), "On Attach", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        Toast.makeText(myActivity.getApplicationContext(), "On Create", Toast.LENGTH_SHORT).show();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
    {
        Toast.makeText(myActivity.getApplicationContext(), "On Create View", Toast.LENGTH_SHORT).show();

        View view = inflater.inflate(R.layout.fragment_1,container, false);
        mGridView = (GridView)view.findViewById(R.id.Grid1);

        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) 
    {
        Toast.makeText(myActivity.getApplicationContext(), "On Activity Created", Toast.LENGTH_SHORT).show();
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void onStart() 
    {
        super.onStart();
        mGridView.setAdapter(new GridBaseAdapter(myActivity));
        mGridView.setOnItemClickListener(this);
    }

    private class GridBaseAdapter extends BaseAdapter 
    {
        LayoutInflater mLayoutInflater = null;
        public GridBaseAdapter(Context mContext) 
        {
            mContext = myActivity;
            mLayoutInflater = LayoutInflater.from(mContext);
        }

        @Override
        public int getCount() 
    {
        return ListItem.length;
    }

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

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

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

        if(convertView == null) 
        {
            convertView = mLayoutInflater.inflate(R.layout.row_fragment_list, null);
        }

        ImageView mImageView = (ImageView) convertView.findViewById(R.id.img_view);
        mImageView.setImageResource(imgID[position]);

        TextView tvUserEmail = (TextView) convertView.findViewById(R.id.text2);
        tvUserEmail.setText("Sub " +ListItem[position]);            

        return convertView;
    }
}

@Override
public void onItemClick(AdapterView<?> arg0, View view, int position, long id) 
{
    TextView txt3 = (TextView)myActivity.findViewById(R.id.tv_fragment3);
    txt3.setText("1st Fragment's : " + position +" Item Clicked");
}
}

我的 Fragment2.class

public class Fragment2 extends ListFragment
{
    Activity myActivity;
    private String ListItem[] = {"Item 1","Item 2","Item 3","Item 4","Item 5","Item 6","Item 6","Item 7"};

@Override
public void onAttach(Activity activity) 
{
    super.onAttach(activity);
    this.myActivity = activity;
    Toast.makeText(activity.getApplicationContext(), "On Attach", Toast.LENGTH_SHORT).show();
}

@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    Toast.makeText(myActivity.getApplicationContext(), "On Create", Toast.LENGTH_SHORT).show();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
{
    Toast.makeText(myActivity.getApplicationContext(), "On Create View", Toast.LENGTH_SHORT).show();

     /** Creating an array adapter to store the list of countries **/
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(inflater.getContext(), android.R.layout.simple_list_item_1,ListItem);
    /** Setting the list adapter for the ListFragment */
    setListAdapter(adapter);

    return super.onCreateView(inflater, container, savedInstanceState);
}

@Override
public void onActivityCreated(Bundle savedInstanceState) 
{
    Toast.makeText(myActivity.getApplicationContext(), "On Activity Created", Toast.LENGTH_SHORT).show();
    super.onActivityCreated(savedInstanceState);
}

@Override
public void onListItemClick(ListView l, View v, int position, long id) 
{
    Toast.makeText(myActivity.getApplicationContext(), position +" Item Clicked", Toast.LENGTH_SHORT).show();
    TextView txt3 = (TextView)myActivity.findViewById(R.id.tv_fragment3);
    txt3.setText("2nd Fragment's : " + position +" Item Clicked");
    super.onListItemClick(l, v, position, id);
}
}

最后我的 Fragment3.class

public class Fragment3 extends Fragment
{
Activity myActivity;
TextView txt_view;  
@Override
public void onAttach(Activity activity) 
{
    super.onAttach(activity);
    this.myActivity = activity;
    Toast.makeText(activity.getApplicationContext(), "On Attach", Toast.LENGTH_SHORT).show();
}

@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    Toast.makeText(myActivity.getApplicationContext(), "On Create", Toast.LENGTH_SHORT).show();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
{
    Toast.makeText(myActivity.getApplicationContext(), "On Create View", Toast.LENGTH_SHORT).show();
    View view = inflater.inflate(R.layout.fragment_3,container, false);
    txt_view = (TextView)view.findViewById(R.id.tv_fragment3);
    return view;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) 
{
    Toast.makeText(myActivity.getApplicationContext(), "On Activity Created", Toast.LENGTH_SHORT).show();
    super.onActivityCreated(savedInstanceState);
}
} 
4

1 回答 1

5

使片段公开您的活动可以附加到的接口。该事件应在需要时发生(例如,单击列表项)。然后,活动应该将片段的实例放在它应该在的地方,例如:

// fragment is an instance of the fragment you want to show
final FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.fragment3, fragment);
transaction.commit();
于 2013-02-14T11:00:08.690 回答