Android Fragment小试一下

学习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;
}
}

功能简单,只在学习!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值