您的位置:首页 > 移动开发 > Android开发

Android Fragment小试一下

2015-10-15 15:03 465 查看
学习Android之余,用Fragment实现了一个效果,左边一个ListView,右边一个Fragment,点击ListView中的每一项,右边切换不同的界面,实现了Fragment的经典实用案例,关键代码如下:

fragment_contain布局:

<?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:baselineAligned="false"

android:orientation="horizontal" >

<ListView

android:id="@+id/fragment_contain_list"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:layout_weight="2"

android:choiceMode="singleChoice"

android:divider="@null"

android:dividerHeight="3dp"

android:background="@drawable/round_corner_nobg"

android:layout_margin="5dp"

android:padding="5dp"

android:listSelector="@android:color/transparent" >

</ListView>

<FrameLayout

android:id="@+id/fragment_contain_show"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:layout_weight="1" >

</FrameLayout>

</LinearLayout>

FragmentContain.java代码:

public class ContainFragment extends Fragment implements OnItemClickListener{

private static final String TAG = ContainFragment.class.getSimpleName();

private View view;

private ContainFragmentAdapter adapter = null;

private ShowFragment firstFragment, secondFragment, thirdFragment,

fourthFragment, fifthFragment;

private ArrayList<String> categoryName = new ArrayList<String>() {

/**

*

*/

private static final long serialVersionUID = 1L;

{

add("First");

add("Second");

add("Third");

add("Fourth");

add("Fifth");

}

};

@Override

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

// TODO Auto-generated method stub

view = inflater.inflate(R.layout.fragment_contain, null);

return view;

}

@Override

public void onActivityCreated(Bundle savedInstanceState) {

// TODO Auto-generated method stub

super.onActivityCreated(savedInstanceState);

ListView categoryList = (ListView) view.findViewById(R.id.fragment_contain_list);

adapter = new ContainFragmentAdapter(getActivity(), categoryName);

categoryList.setAdapter(adapter);

categoryList.setOnItemClickListener(this);

firstFragment = new ShowFragment();

Bundle bundle = new Bundle();

bundle.putString("content", "First");

firstFragment.setArguments(bundle);

getChildFragmentManager().beginTransaction().replace(R.id.fragment_contain_show, firstFragment).commit();

}

@Override

public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

// TODO Auto-generated method stub

if(parent.getId() == R.id.fragment_contain_list) {

if(adapter.setCurrentPosition(position)) return;

adapter.notifyDataSetChanged();

Log.i(TAG, "position:"+position);

switch (position) {

case 0:

if(firstFragment == null) {

firstFragment = new ShowFragment();

Bundle bundle = new Bundle();

bundle.putString("content", "First");

firstFragment.setArguments(bundle);

}

getChildFragmentManager().beginTransaction().replace(R.id.fragment_contain_show, firstFragment).commit();

break;

case 1:

if(secondFragment == null) {

secondFragment = new ShowFragment();

Bundle bundle = new Bundle();

bundle.putString("content", "Second");

secondFragment.setArguments(bundle);

}

getChildFragmentManager().beginTransaction().replace(R.id.fragment_contain_show, secondFragment).commit();

break;

case 2:

if(thirdFragment == null) {

thirdFragment = new ShowFragment();

Bundle bundle = new Bundle();

bundle.putString("content", "Third");

thirdFragment.setArguments(bundle);

}

getChildFragmentManager().beginTransaction().replace(R.id.fragment_contain_show, thirdFragment).commit();

break;

case 3:

if(fourthFragment == null) {

fourthFragment = new ShowFragment();

Bundle bundle = new Bundle();

bundle.putString("content", "Fourth");

fourthFragment.setArguments(bundle);

}

getChildFragmentManager().beginTransaction().replace(R.id.fragment_contain_show, fourthFragment).commit();

break;

case 4:

if(fifthFragment == null) {

fifthFragment = new ShowFragment();

Bundle bundle = new Bundle();

bundle.putString("content", "Fifth");

fifthFragment.setArguments(bundle);

}

getChildFragmentManager().beginTransaction().replace(R.id.fragment_contain_show, fifthFragment).commit();

break;

default:

break;

}

}

}

}

ContainFragmentAdapter.java代码

public class ContainFragmentAdapter extends BaseAdapter{

private LayoutInflater layoutInflater;

private ArrayList<String> names;

private int currentPosition = 0;

public ContainFragmentAdapter(Context context, ArrayList<String> list) {

layoutInflater = LayoutInflater.from(context);

names = list;

}

@Override

public int getCount() {

// TODO Auto-generated method stub

return names.size();

}

@Override

public Object getItem(int position) {

// TODO Auto-generated method stub

return names.get(position);

}

@Override

public long getItemId(int position) {

// TODO Auto-generated method stub

return position;

}

@Override

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

// TODO Auto-generated method stub

ViewHolder holder = null;

if(convertView == null) {

holder = new ViewHolder();

convertView = layoutInflater.inflate(R.layout.adapter_fragment_contain, null);

holder.name = (TextView) convertView.findViewById(R.id.adapter_fragment_contain_name);

convertView.setTag(holder);

} else holder = (ViewHolder) convertView.getTag();

if(position == currentPosition) {

convertView.setBackgroundResource(R.drawable.item_click_bg);

} else convertView.setBackgroundResource(0);

String one = names.get(position);

if(one != null) holder.name.setText(one);

return convertView;

}

public boolean setCurrentPosition(int position) {

if(currentPosition == position) return true;

else {

currentPosition = position;

return false;

}

}

private class ViewHolder {

private TextView name;

}

}

功能简单,只在学习!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: