Android Fragment 简洁版 list
16lz
2021-01-23
<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:orientation="vertical" tools:context="com.example.fragmentapp.MainActivity" > <TextView android:id="@+id/title" android:layout_width="match_parent" android:layout_height="0dip" android:gravity="center" android:textSize="24sp" android:padding="10dip" android:text="标题" android:textColor="@android:color/white" android:layout_weight="2" /> <FrameLayout android:id="@+id/content" android:layout_width="match_parent" android:layout_height="0dip" android:layout_weight="7" /> <LinearLayout android:layout_width="match_parent" android:layout_height="0dip" android:layout_weight="1" android:orientation="horizontal" > <TextView android:id="@+id/message" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center" android:text="消息" android:textSize="15sp" /> <TextView android:id="@+id/contact" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center" android:text="联系人" android:textSize="15sp" /> </LinearLayout></LinearLayout>
package com.example.fragmentapp;import android.app.Fragment;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.TextView;public class MessageFragment extendsFragment{@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {View view=inflater.inflate(android.R.layout.simple_list_item_1,null);returnview;}@Overridepublic void onViewCreated(View view, Bundle savedInstanceState) {TextView text=(TextView) view.findViewById(android.R.id.text1);text.setText("消息界面");}}
package com.example.fragmentapp;import android.app.Fragment;import android.app.ListFragment;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.ArrayAdapter;import android.widget.TextView;public class ContactFragment extendsListFragment{//@Override//public View onCreateView(LayoutInflater inflater, ViewGroup container,//Bundle savedInstanceState) {////View view=inflater.inflate(android.R.layout.simple_list_item_1,null);//TextView text=(TextView) view.findViewById(android.R.id.text1);//text.setText("联系人界面");////returnview;//}@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);String[] data=new String[100];for(int i=0;i<data.length;i++){data[i]="联系人:"+i;}ArrayAdapter adapter=new ArrayAdapter(getActivity(),android.R.layout.simple_list_item_1,data);this.setListAdapter(adapter);}}
package com.example.fragmentapp;import android.app.Activity;import android.app.Fragment;import android.app.FragmentManager;import android.app.FragmentTransaction;import android.graphics.Color;import android.os.Bundle;import android.view.View;import android.widget.TextView;public class MainActivity extends Activity {private staticint POSITION=-1;privateTextView[] texts=new TextView[2];privateFragment[] fragments=new Fragment[2];privateTextView title;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);title=(TextView) findViewById(R.id.title);title.setBackgroundColor(Color.BLUE);texts[0]=(TextView) findViewById(R.id.message);texts[1]=(TextView) findViewById(R.id.contact);fragments[0]=new MessageFragment();fragments[1]=new ContactFragment();choose(0);addTextViewListener(texts[0], 0);addTextViewListener(texts[1], 1);}privatevoidaddTextViewListener(TextView text,finalint pos){text.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {choose(pos);}});}privatevoidchoose(int pos){//重复的点击if(pos==POSITION)return;for(int i=0;i<texts.length;i++){//选中if(pos==i){texts[i].setTextColor(Color.RED);texts[i].setBackgroundColor(Color.LTGRAY);title.setText(texts[i].getText()+"");loadFragment(fragments[i]);}//未被选中else{texts[i].setTextColor(Color.DKGRAY);texts[i].setBackgroundColor(Color.WHITE);}}POSITION=pos;}privatevoidloadFragment(Fragment f){FragmentManager fm=this.getFragmentManager();FragmentTransaction ft=fm.beginTransaction();ft.replace(R.id.content, f);ft.commit();}}
更多相关文章
- Android联系人1--分组查询
- 四、[Android UI界面] android继承关系图
- 登录界面(Android studio)
- android欢迎界面并执行任务
- Android APP--建立简单的交互界面
- Android中编码实现软件界面
- Android 通信录中的联系人操作
- android原生音乐播放器界面字体显示不全
- android多选联系人实现