文章目录
竖屏时弹出新内容页面,横屏时在右侧显示内容
一、案例演示
竖屏
横屏
二、Activity页面
1、activity_main.xml
放了两个fragment控件,左边用于显示listview列表,右边用于显示内容
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<fragment
android:id="@+id/fragment"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:name="com.example.fragment2.BlankFragment"></fragment>
<fragment
android:id="@+id/fragment2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:name="com.example.fragment2.BlankFragment2">
</fragment>
</LinearLayout>
2、MainActivity.java
不在mainactivity中操作,使代码简洁
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
三、Fragment页面
1、fragment_blank.xml
左侧fragment实现listview 列表
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
tools:context=".BlankFragment">
<ListView
android:id="@+id/listview1"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
</FrameLayout>
2、BlankFragment.java
思路整理
利用实体类,自定义适配器,自定义布局,给listview设置数据,当点击listview列表时显示对应的内容,
判断横竖屏,横屏显示右侧的内容页面,竖屏直接跳转新页面
public class BlankFragment extends Fragment {
private ListView listView;
private NewsAdapter adapter;
private List<News> list;
//当Fragment与Activity发生关联时调用,利用上下文参数
@Override
public void onAttach(@NonNull Context context) {
super.onAttach(context);
list=new ArrayList<>();
list.add(new News("路飞一档","遇到迷茫时,任何人都会变得软弱。一旦坚信自己可以帮到别人,他们就会变得很强大",R.drawable.l1));
list.add(new News("路飞二档","我不管这个世上的人怎么说我,只想依照我的信念做事,绝不后悔,不管现在将来都一样",R.drawable.l2));
adapter=new NewsAdapter(context,R.layout.news_item,list);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view= inflater.inflate(R.layout.fragment_blank, container, false);
listView=view.findViewById(R.id.listview1);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
News news = list.get(position);
//获取到主布局中的fragment2,内容其实就是BlankFragment2
BlankFragment2 fragment2 = (BlankFragment2) getFragmentManager().findFragmentById(R.id.fragment2);
//获取当前屏幕状态
Configuration configuration= getResources().getConfiguration();
int ori=configuration.orientation;
//判断横竖屏
if(ori==configuration.ORIENTATION_LANDSCAPE){
//横屏
fragment2.refash(news.getTitle(), news.getContent());
}else if(ori==configuration.ORIENTATION_PORTRAIT){
//竖屏
actionstart(getActivity(),news);
}
}
});
return view;
}
}
2.1、News.java
implements Serializable j才可以进行实体类信息传递
public class News implements Serializable {
private String title;
private String content;
private int image;
public News(){
}
public News(String title,String content,int image){
this.title = title;
this.content = content;
this.image = image;
}
public String getTitle() {
return title;
}
public String getContent() {
return content;
}
public int getImage() {
return image;
}
public void setTitle(String title) {
this.title = title;
}
public void setContent(String content) {
this.content = content;
}
public void setImage(int image) {
this.image = image;
}
}
2.1、NewsAdapter.java
public class NewsAdapter extends ArrayAdapter<News> {
private int resourceId;
public NewsAdapter(Context context, int textViewResourceId, List<News> objects) {
super(context, textViewResourceId, objects);
// TODO Auto-generated constructor stub
resourceId = textViewResourceId;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View view;
News news = getItem(position);
if( convertView == null)
{
view = LayoutInflater.from(getContext()).inflate(resourceId, null);
}else
{
view = convertView;
}
ImageView image = (ImageView)view.findViewById(R.id.titleimage);
image.setImageResource(news.getImage());
TextView title = (TextView)view.findViewById(R.id.title);
title.setText(news.getTitle());
return view;
}
}
2.1、News_item.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="horizontal"
>
<ImageView
android:id="@+id/titleimage"
android:layout_width="50dp"
android:layout_height="50dp"/>
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:ellipsize="end"
android:textSize="25dp"/>
</LinearLayout>
3、fragment_blank2.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/newstitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25dp"
android:layout_gravity="center"></TextView>
<TextView
android:id="@+id/newscontent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25dp"></TextView>
</LinearLayout>
4、BlankFragment2.java
定义refash方法用于给控件赋值
定义actionstart方法用于竖屏页面跳转
public class BlankFragment2 extends Fragment {
View view;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view= inflater.inflate(R.layout.fragment_blank2, container, false);
return view;
}
public void refash(String title,String content){
TextView title1=view.findViewById(R.id.newstitle);
TextView content1=view.findViewById(R.id.newscontent);
title1.setText(title);
content1.setText(content);
}
public static void actionstart(Context context,News news){
Intent intent=new Intent(context,NewsContextActivity.class);
intent.putExtra("news", news);
context.startActivity(intent);
}
}
三、竖屏弹出新页面
1、activity_news_context
新的activity页面,用于竖屏跳转新页面
<?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">
<fragment
android:id="@+id/fragment3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.example.fragment2.BlankFragment2"></fragment>
</LinearLayout>
2、NewsContextActivity
获取到当判断竖屏时,把数据存储后传输过来,接收后给activity控件赋值
public class NewsContextActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_news_context);
Intent intent =getIntent();
News news=(News) intent.getSerializableExtra("news");
//获取BlankFragment2对象
FragmentManager manager=getSupportFragmentManager();
//获取到内容布局中的fragment,内容其实就是BlankFragment2
BlankFragment2 fragment3=(BlankFragment2) manager.findFragmentById(R.id.fragment3);
//把数据放到fragment2内容布局上
fragment3.refash(news.getTitle(),news.getContent());
}
}