随着手机不同尺寸的推出,加上平板市场的火热,很多软件开始做左侧导航,右侧详情,这样可以方便我们更加直观的看到详情。
例如这样:
下面就用简洁明了的代码来说怎样实现的吧!
直接上步骤
1.在AndroidManifest里面先把Activity设置成横屏显示
加粗这块放进去即可,当然不设置也没事哈
这句话:
android:screenOrientation=”landscape”
<activity android:name=".MainActivity"
android:screenOrientation="landscape"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
2.再去写布局啦
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.holly.mytest4">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity"
android:screenOrientation="landscape"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
再写左侧的fragment布局,取名:left_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:orientation="vertical"
>
<ListView
android:id="@+id/lv1"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
</ListView>
</LinearLayout>
右侧显示详情的fragment布局,取名right_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tv"
android:text="测试字体"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textSize="20sp"
/>
</LinearLayout>
好了,到这里布局就都写完了
然后创建一个实体,叫做:entity.class,代码如下
package com.holly.mytest4;
/**
* Created by Administrator on 2017/1/4.
*/
public class entity {
private String title ;
private String content ;
public entity(String title, String content) {
this.title = title;
this.content = content;
}
public entity() {
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
@Override
public String toString() {
return title;
}
}
左侧的fragment绑定布局,创建一个LeftFragment.class
package com.holly.mytest4;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import com.holly.mytest4.R;
import com.holly.mytest4.entity;
import java.util.ArrayList;
/**
* Created by Administrator on 2017/1/3.
*/
public class LeftFragment extends Fragment {
ListView lv ;
ArrayList<entity> datas = new ArrayList<entity>() ;
ArrayAdapter<entity> adapter ;
View view ;
ListViewItemClickListener listViewItemClickListener ;
/**
* 在该方法中绑定布局,初始化控件,绑定监听器
* @param inflater
* @param container
* @param savedInstanceState
* @return 返回的就是当前fragment布局转换成的View对象
*/
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
System.out.println("container-->"+container);
if(view == null)
{
view = inflater.inflate(R.layout.left_layout,null);
lv = (ListView) view.findViewById(R.id.lv1);
//通过getActivity方法得到该fragment所依赖的Activity对象
System.out.println("getActivity()-->"+getActivity());
adapter = new ArrayAdapter<entity>(getActivity(),android.R.layout.simple_list_item_1,datas);
//设置适配器
lv.setAdapter(adapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//将指定位置的数据设置给listViewItemClickListener
listViewItemClickListener.onItemClick(datas.get(position).getContent());
}
});
}
return view;
}
//在该方法中加载数据
@Override
public void onStart() {
super.onStart();
getData();
adapter.notifyDataSetChanged();
}
private void getData()
{
entity entry = new entity("Android","三星,华为,小米,htc等");
datas.add(entry);
entry = new entity("IOS","iphone,ipad");
datas.add(entry);
entry = new entity("WP","诺基亚,htc,华为");
datas.add(entry);
}
public interface ListViewItemClickListener{
public void onItemClick(String content);
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
listViewItemClickListener = (ListViewItemClickListener) context;
}
}
右侧的同样也需要绑定,创建一个RightFragment.class,当然这里我自己加了一些生命周期的方法,为了验证生命周期,我测试了一下,可以删掉哦
package com.holly.mytest4;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.holly.mytest4.R;
/**
* Created by Administrator on 2017/1/3.
*/
public class RightFragment extends Fragment {
View view ;
TextView tv ;
//当该Fragment与Activity发生关联的一瞬间触发的方法
@Override
public void onAttach(Context context) {
super.onAttach(context);
System.out.println("RightFragment-->onAttach-->"+context);
}
//创建,实例化
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
System.out.println("RightFragment-->onCreate");
}
//创建视图
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
if(view == null)
{
view = inflater.inflate(R.layout.right_layout,null);
tv = (TextView) view.findViewById(R.id.tv);
}
System.out.println("RightFragment-->onCreateView");
return view;
}
//提供给TextView设置数据的方法
public void setTextViewText(String text)
{
tv.setText(text);
}
//当与之绑定的Activity的onCreate方法执行结束之后触发的方法
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
System.out.println("RightFragment-->onActivityCreated");
}
@Override
public void onStart() {
super.onStart();
System.out.println("RightFragment-->onStart");
}
@Override
public void onResume() {
super.onResume();
System.out.println("RightFragment-->onResume");
}
@Override
public void onStop() {
super.onStop();
System.out.println("RightFragment-->onStop");
}
@Override
public void onPause() {
super.onPause();
System.out.println("RightFragment-->onPause");
}
//摧毁视图
@Override
public void onDestroyView() {
super.onDestroyView();
System.out.println("RightFragment-->onDestroyView");
}
@Override
public void onDestroy() {
super.onDestroy();
System.out.println("RightFragment-->onDestroy");
}
//当当前Fragment与Activity解除联系的一瞬间触发的方法
@Override
public void onDetach() {
super.onDetach();
System.out.println("RightFragment-->onDetach");
}
}
最后一部,就是写MainActivity了,代码如下:
package com.holly.mytest4;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.os.Bundle;
public class MainActivity extends FragmentActivity implements LeftFragment.ListViewItemClickListener{
RightFragment rightFragment ;
FragmentManager fm ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
System.out.println("MainActivity-->onCreate");
}
private void init() {
fm = getSupportFragmentManager();
//通过FragmentManager找到需要操作的fragment
rightFragment = (RightFragment) fm.findFragmentById(R.id.right_fragment);
}
@Override
protected void onStart() {
super.onStart();
System.out.println("MainActivity-->onStart");
}
@Override
protected void onResume() {
super.onResume();
System.out.println("MainActivity-->onResume");
}
@Override
protected void onPause() {
super.onPause();
System.out.println("MainActivity-->onPause");
}
@Override
protected void onStop() {
super.onStop();
System.out.println("MainActivity-->onStop");
}
@Override
protected void onDestroy() {
super.onDestroy();
System.out.println("MainActivity-->onDestroy");
}
@Override
public void onItemClick(String content) {
rightFragment.setTextViewText(content);
}
}
好了,到这里就写完了,注意看哦,MainActivity是实现了LeftFragment里面的ListViewItemClickListener方法,千万不要漏掉了,最后附上我的项目结构图,因为只是测试,所以没有把fragment分类哦