看下我实现的效果图这种真的特别常见点击左边的item替换右边的布局文件
package view.zm.com.wyimtest.bean;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Created by zhengmin on 2017/1/11.
*/
public class MoiveContent {
//定义一个内部类,作为系统的业务对象
public static class Movie {
public Integer id;
public String title;
public String desc;
public Movie(Integer id, String title, String desc) {
this.id = id;
this.title = title;
this.desc = desc;
}
@Override
public String toString() {
return title;
}
}
//使用List集合记录系统所包含的Movie对象
public static List ITEMS = new ArrayList<>();
//使用Map集合记录系统所包含的Movie对象
public static Map ITEM_MAP = new HashMap<>();
static {
//使用静态初始化代码,将Movie对象添加到List集合、Map集合中
addItem(new Movie(1, "蒼井空", "这个程序员没呢应该都很清楚的哈"));
addItem(new Movie(2, "波多野結衣", "这个宅男你们懂的哈哈"));
addItem(new Movie(3, "咩亞洲", "老司机带带我,我得意的笑"));
addItem(new Movie(4, "飞碟一分钟", "只是从未如此性感,飞碟一分钟开开心心"));
addItem(new Movie(5, "锦绣未央", "代表月亮消灭您真的太美了哈"));
addItem(new Movie(6, "七月与安生", "周冬雨与韩国欧巴一场美丽爱情的邂逅"));
addItem(new Movie(7, "人在囧途", "王宝强之间的囧事真的好搞笑哈"));
addItem(new Movie(8, "微微一笑很倾城", "杨洋与郑爽之间的甜蜜校园爱情"));
addItem(new Movie(9, "猫与老鼠", "主要讲述Tom猫经常被Jaliy鼠整搞笑。"));
addItem(new Movie(10, "美国队长3", "这部电影挺不错哦。"));
addItem(new Movie(11, "放棄我抱緊我", "主要讲述王凯和乔任梁以及陈之间的三角恋中"));
// addItem(new Movie(12, "那些年我们一起追过的女孩", "一本全面、深入的iOS学习书籍,從入門到搬磚。"));
}
private static void addItem(Movie movie) {
ITEMS.add(movie);
ITEM_MAP.put(movie.id, movie);
}
}
package view.zm.com.wyimtest.fragment;
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import view.zm.com.wyimtest.R;
import view.zm.com.wyimtest.bean.MoiveContent;
/**
* Created by zhengmin on 2017/1/11.
*/
public class MovieDetailFragment extends Fragment {
public static final String ITEM_ID="item_id";
private MoiveContent.Movie movie;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//如果启动该Fragment时包含了ITEM_ID参数
if(getArguments().containsKey(ITEM_ID)){
movie= MoiveContent.ITEM_MAP.get(getArguments().get(ITEM_ID));
}
}
//重写该方法,该方法返回的View将作为Fragment显示的组件
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView=inflater.inflate(R.layout.fragment_movie_detail,container,false);
if(movie!=null){
((TextView) rootView.findViewById(R.id.movie_title)).setText(movie.title);
((TextView) rootView.findViewById(R.id.movie_desc)).setText(movie.desc);
}
return rootView;
}
}
package view.zm.com.wyimtest.fragment;
import android.app.Activity;
import android.app.ListFragment;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import view.zm.com.wyimtest.bean.MoiveContent;
/**
* Created by zhengmin on 2017/1/11.
*/
public class MovieListFragment extends ListFragment {
private Callbacks mCallbacks;
//定义一个回调接口,该Fragment所在Activity需要实现该接口
//该Fragment将通过该接口与它所在的Activity交互
public interface Callbacks{
public void onItemSelected(Integer id);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//为该ListFragment设置adapter
setListAdapter(new ArrayAdapter(getActivity(),android.R.layout.simple_list_item_activated_1, MoiveContent.ITEMS));
}
//当该Fragment被添加、显示到Activity时,回调该方法
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
//如果Activity没有实现Callbacks接口,抛出异常
if(!(activity instanceof Callbacks)){
throw new IllegalArgumentException("BookList所在Activity实现CallBack");
}
//把该Activity当成Callbacks对象
mCallbacks= (Callbacks) getActivity();
}
//当该Fragment从它所属的Activity中被删除时回调该方法
@Override
public void onDetach() {
super.onDetach();
mCallbacks=null;
}
//当用户单击某列表时激发该回调方法
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
//激发mCallbacks的onItemSelected方法
mCallbacks.onItemSelected(MoiveContent.ITEMS.get(position).id);
}
public void setActivateOnItemClick(boolean activateOnItemClick){
getListView().setChoiceMode(activateOnItemClick?ListView.CHOICE_MODE_SINGLE:ListView.CHOICE_MODE_NONE);
}
}
package view.zm.com.wyimtest;
import android.app.Activity;
import android.os.Bundle;
import view.zm.com.wyimtest.fragment.MovieDetailFragment;
import view.zm.com.wyimtest.fragment.MovieListFragment;
public class MainActivity extends Activity implements MovieListFragment.Callbacks {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_movie_twopane);
}
@Override
public void onItemSelected(Integer id) {
//创建Bundle对象准备向Fragment传参
Bundle arguments=new Bundle();
arguments.putInt(MovieDetailFragment.ITEM_ID,id);
//创建MovieDetailFragment对象
MovieDetailFragment movieDetailFragment =new MovieDetailFragment();
//向fragment传参
movieDetailFragment.setArguments(arguments);
//使用fragment替换movie_detail_container容器当前显示的Fragment
getFragmentManager().beginTransaction().replace(R.id.movie_detail_container, movieDetailFragment).commit();
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:divider="?android:attr/dividerHorizontal"
android:orientation="horizontal"
android:showDividers="middle"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="@+id/movie_list"
android:name="view.zm.com.wyimtest.fragment.MovieListFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"/>
<FrameLayout
android:id="@+id/movie_detail_container"
android:layout_width="0dp"
android:layout_weight="3"
android:layout_height="match_parent">FrameLayout>
LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:orientation="vertical"
android:padding="20dp"
>
<TextView
android:id="@+id/movie_title"
android:layout_width="match_parent"
style="?android:attr/textAppearanceSmall"
android:textSize="18sp"
android:textColor="@color/colorPrimary"
android:layout_height="wrap_content"
android:padding="16dp"/>
<TextView
android:id="@+id/movie_desc"
style="?android:attr/textAppearanceSmall"
android:layout_width="match_parent"
android:textSize="18sp"
android:gravity="center_horizontal"
android:textColor="@color/colorAccent"
android:layout_height="wrap_content"
android:padding="16dp"/>
LinearLayout>
今晚到这里到时在慢慢分析代码,好困转载请注明出处
http://blog.csdn.net/qq_15950325/article/details/54356243这里写链接内容
需要开车的直接加群