Android-(10)碎片实战

新闻实体类:

public class News {
    private String title;   //标题
    private String content;   //n内容

    public String getContent() {
        return content;
    }

    public String getTitle() {
        return title;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public void setTitle(String title) {
        this.title = title;
    }
}

上面标题,下面内容的布局content_title_layout.xml :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <LinearLayout
        android:id="@+id/visibility_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:visibility="invisible">

        <TextView
            android:id="@+id/news_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:padding="25dp"
            android:textSize="20dp"
            android:background="#CBEEFF"
            android:layout_margin="5dp"/>

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="#000"></View>

        <TextView
            android:id="@+id/news_context"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:padding="15dp"
            android:textSize="18sp" />
    </LinearLayout>

    <View
        android:layout_width="1dp"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:background="#000"></View>
</RelativeLayout>

通过上面的布局创建碎片:

public class NewsContentFragment extends Fragment {
    private View view;

    public View onCreateView(LayoutInflater layoutInflater, ViewGroup container, Bundle saveInstanceState)
    {
        view = layoutInflater.inflate(R.layout.content_title_layout, container, false);
        return  view;
    }

    public void refresh(String newsTitle, String newsContent)
    {
        View visibilityLayout = view.findViewById(R.id.visibility_layout);
        visibilityLayout.setVisibility(View.VISIBLE);
        TextView newsTitleText = (TextView)view.findViewById(R.id.news_title);
        TextView newsContentText = (TextView)view.findViewById(R.id.news_context);
        newsTitleText.setText(newsTitle);
        newsContentText.setText(newsContent);
    }
}

上面都是在双页中使用,要在单页中使用也就是在手机中要再创建一个活动:

public class NewsContentActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.news_content);

        String newsTitle = getIntent().getStringExtra("news_title");
        String newsContent = getIntent().getStringExtra("news_content");
        NewsContentFragment newsContextFragment = (NewsContentFragment)getSupportFragmentManager().findFragmentById(R.id.news_content_fragment);
        newsContextFragment.refresh(newsTitle, newsContent);
    }
    public static void actionStart(Context context, String title, String content)
    {
        Intent intent = new Intent(context, NewsContentActivity.class);
        intent.putExtra("news_title", title);
        intent.putExtra("news_content", content);
        context.startActivity(intent);
    }
}
<?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"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

<fragment
    android:id="@+id/news_content_fragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:name="com.example.myapplication.NewsContentFragment"
    />
</LinearLayout>

然后新建一个布局,用于存放文章标题列表news_title_frag.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">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/news_title_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

子项布局news_item.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="wrap_content">

    <TextView
        android:id="@+id/news_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:singleLine="true"
        android:ellipsize="end"
        android:textSize="18dp"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:paddingTop="15dp"
        android:paddingBottom="15dp"
        android:background="#FFCBCB"
        android:layout_margin="5dp"
        />
</LinearLayout>

展示新闻列表的碎片

xupublic class NewsTitleFragment extends Fragment {
    private boolean isTowPane;

    public View onCreateView(LayoutInflater layoutInflater, ViewGroup container, Bundle saveInstanceState)
    {
        View view = layoutInflater.inflate(R.layout.content_title_layout, container, false);
        return  view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if(getActivity().findViewById(R.id.news_content_fragment) != null)
        {
            isTowPane = true;
        }
        else
        {
            isTowPane = false;
        }
    }
}

修改主活动布局:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <fragment
        android:id="@+id/news_title_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:name="com.example.myapplication.NewsTitleFragment"/>
</FrameLayout>

然后再res下新建layout_sw600dp文件夹,再新建同名布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <fragment
        android:id="@+id/news_title_layout"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="match_parent"
        android:name="com.example.myapplication.NewsTitleFragment"
        />


    <FrameLayout
        android:id="@+id/news_content_layout"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="3">

        <fragment
            android:id="@+id/news_content_fragment"
            android:name="com.example.myapplication.NewsContentFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    </FrameLayout>

</LinearLayout>

那么在手机和平板上就会有不同的显示。

修改:NewsTitleFragment 碎片

public class NewsTitleFragment extends Fragment {

    private boolean isTowPane;

    class NewsAdapter extends RecyclerView.Adapter<NewsAdapter.ViewHolder>
    {
        private List<News> mNewsList;

        class ViewHolder extends RecyclerView.ViewHolder
        {
            TextView newsTitleText;
            public ViewHolder(View view)
            {
                super(view);
                newsTitleText = (TextView)view.findViewById(R.id.news_title);
            }
        }
        public NewsAdapter(List<News> mNewsList)
        {
            this.mNewsList = mNewsList;
        }

        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.news_item, parent, false);
            final ViewHolder holder = new ViewHolder(view);
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    News news = mNewsList.get(holder.getLayoutPosition());
                    if(isTowPane)
                    {
                        NewsContentFragment newsContentFragments = (NewsContentFragment)getFragmentManager().findFragmentById(R.id.news_content_fragment);
                        newsContentFragments.refresh(news.getTitle(), news.getContent());
                    }
                    else
                    {
                        NewsContentActivity.actionStart(getActivity(), news.getTitle(), news.getContent());
                    }
                }
            });
            return holder;
        }

        @Override
        public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
            News news = mNewsList.get(position);
            holder.newsTitleText.setText(news.getTitle());
        }

        @Override
        public int getItemCount() {
            return mNewsList.size();
        }
    }

    public View onCreateView(LayoutInflater layoutInflater, ViewGroup container, Bundle saveInstanceState)
    {
        View view = layoutInflater.inflate(R.layout.news_title_frag, container, false);
        RecyclerView newsTitleRecyclerView = (RecyclerView)view.findViewById(R.id.news_title_recycler_view);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        newsTitleRecyclerView.setLayoutManager(linearLayoutManager);
        NewsAdapter newsAdapter = new NewsAdapter(getNews());
        newsTitleRecyclerView.setAdapter(newsAdapter);
        return  view;
    }

    //判断当前是单还是双
    public void onActivityCreated(Bundle saveInstanceState) {
        super.onActivityCreated(saveInstanceState);
        if(getActivity().findViewById(R.id.news_content_layout) != null)
        {
            isTowPane = true;
        }
        else
        {
            isTowPane = false;
        }
    }

    //返回一个数据链表
    private List<News> getNews()
    {
        List<News> newsList = new ArrayList<>();
        for(int i = 0; i <= 50; i++)
        {
            News news = new News();
            news.setTitle("this is news title" + i);
            news.setContent(getRandomLengthContent("this is news content" + i + "."));
            newsList.add(news);
        }
        return newsList;
    }
    
    //返回由标题而重复的内容
    private String getRandomLengthContent(String content)
    {
        Random random = new Random();
        int length = random.nextInt(20) + 1;
        StringBuffer stringBuffer = new StringBuffer();
        for(int i = 0; i < length; i++)
        {
            stringBuffer.append(content);
        }
        return stringBuffer.toString();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值