0
点赞
收藏
分享

微信扫一扫

Fragment动态添加内容


Fragement在3.0以后才有,对于3.0以下的版本可以用FragmentActivty向下兼容,另外强调一点就是这里要引用V4包,也许有一部分引用了V4包还是没有方法,那可能就是V4包的版本低了需要高版本的,为了统一转换最好在Activty里都引用V4包,不然有可能会出问题哦!

实现功能:动态添加内容到Fragment,主要分为两部分1/3(左边占一份,右边占3份),左边为LIstview用适配器进行添加,右边暂时为空,主要代码实现:

第一步:

写一个Fragment1继承Fragment

package com.example.fragment;

import java.util.ArrayList;
import java.util.List;


import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;


public class Fragmnet1 extends android.support.v4.app.Fragment{
private Myadapters myadapters;
private ListView mlListView;
@Override
public Viod onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {


View view=inflater.inflate(R.layout.fragment1, container, false);

mlListView=(ListView) view.findViewById(R.id.listw);
myadapters = new Myadapters(getActivity());
myadapters.setData(getData());
mlListView.setAdapter(myadapters);

return view;
}
//数据源,这里随便添加的数据
public List<Messages> getData() {
List<Messages> list = new ArrayList<Messages>();

Messages msg = new Messages();
msg.setmTextView("美食");


Messages msg1 = new Messages();
msg1.setmTextView("电影");



Messages msg2 = new Messages();
msg2.setmTextView("酒店");


Messages msg3 = new Messages();
msg3.setmTextView("KTV");


Messages msg4 = new Messages();

msg4.setmTextView("今日新单");

Messages msg5 = new Messages();
msg5.setmTextView("代金卷");


Messages msg6 = new Messages();
msg6.setmTextView("周边游");


Messages msg7 = new Messages();
msg7.setmTextView("更多");


list.add(msg);
list.add(msg1);
list.add(msg2);
list.add(msg3);
list.add(msg4);
list.add(msg5);
list.add(msg6);
list.add(msg7);

return list;
}

}

补上对应的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:background="#00ff00" >

<ListView
android:id="@+id/listw"
android:layout_width="wrap_content"
android:layout_height="wrap_content"

android:textColor="#000000"
android:textSize="25sp" />

</LinearLayout>

-------------------------------------------------------------------分割线----------------------------------------------------------------------------

第二步:再写一个Fragent2继承Fragement

package com.example.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


public class Fragmnet2 extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

return inflater.inflate(R.layout.fragment2, container, false);
}
}

同样补上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:background="#ffff00"
android:orientation="vertical" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is fragment 2"
android:textColor="#000000"
android:textSize="25sp" />

</LinearLayout>

----------------------------------------------------------------------------------------------

第三步:写主Activty继承FragmentActivity

package com.example.fragment;


import java.util.ArrayList;
import java.util.List;


import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.widget.ListView;


public class MainFargMent extends FragmentActivity{


@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_farg_ment);

//1.获取FragmentManager对象
FragmentManager manager = getSupportFragmentManager();
//2.获取FragmentTransaction对象
FragmentTransaction transaction = manager.beginTransaction();
//3、添加Fragment对象
//4、动态添加数据源
Fragmnet1 mFragmnet1=new Fragmnet1();
Fragmnet2 mFragmnet2=new Fragmnet2();
//5、对应主Activy里两个Fragment,xml的id,实例化Fragement的名
transaction.add(R.id.relativeLayout1, mFragmnet1);
transaction.add(R.id.relativeLayout2, mFragmnet2);

//6提交
transaction.commit();
}

}

同样补上xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false"
android:orientation="horizontal"
>
<FrameLayout
android:id="@+id/relativeLayout1"
android:name="com.example.fragment.Fragmnet1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
>
</FrameLayout>
<FrameLayout
android:id="@+id/relativeLayout2"
android:name="com.example.fragment.Fragmnet2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"
>

</FrameLayout>

</LinearLayout>

-----------------------------------------------分割线-------------------------------------------

第四步:写一个适配器,这里适配器大家可以随便自定义,为了快速测试建议用系统的适配器

package com.example.fragment;


import java.util.ArrayList;
import java.util.List;


import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;


public class Myadapters extends BaseAdapter {
private List<Messages> mList = new ArrayList<Messages>();
private LayoutInflater mInflater;
private Context mcontext;


public Myadapters(Context context) {
mcontext = context;
mInflater = LayoutInflater.from(mcontext);
}
public void setData(List<Messages> mList){
this.mList=mList;
for(Messages message : mList){
Log.v("tag","setData >>>>>>>>> "+message.getmTextView());
}
notifyDataSetChanged();
}
@Override
public int getCount() {
Log.v("tag","size"+mList.size());
// TODO Auto-generated method stub
return mList.size();


}


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


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


@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();


convertView = mInflater.inflate(R.layout.myadapter, null);



holder.titleTxt = (TextView) convertView
.findViewById(R.id.textv);

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


Messages msg = (Messages)getItem(position);
Log.v("tag", "getView >>>>>>>>>>"+msg.getmTextView());

holder.titleTxt.setText(msg.getmTextView());

return convertView;
}


class ViewHolder {

TextView titleTxt = null;

}

}
package com.example.fragment;


import android.widget.ImageView;
import android.widget.TextView;


public class Messages {
private int mimgage;
private String mTextView;
public int getMimgage() {
return mimgage;
}
public void setMimgage(int mimgage) {
this.mimgage = mimgage;
}
public String getmTextView() {
return mTextView;
}
public void setmTextView(String mTextView) {
this.mTextView = mTextView;
}
}

还是补上listview里的布局testview的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" >

<TextView
android:id="@+id/textv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>

一个简单的动态添加数据就完成了,这就是今天所学的课程。



举报

相关推荐

0 条评论