KnightBubble / android-notes

android-notes
0 stars 1 forks source link

点击左侧fragment内容在右侧展示 #3

Open KnightBubble opened 9 years ago

KnightBubble commented 9 years ago
package com.qianfeng.fragmenttest;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
/**
 * 需要做什么?
 * 1.将电影名称列表显示到listview中
 * 2.绑定电影列表的点击事件  
 *   点击其中某项item时将该电影所对应的assets文件夹下的文件进行解析并且展示到rightfrgment中的textview中
 */
public class LeftFragment extends Fragment {
    private ListView lv;
    private String[] movies={"扫毒","功夫战斗机","顶楼的大象","狂爱恶徒","兽性窥视","猩球崛起"};
    private ArrayAdapter<String> adapter;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.left_fragment, null);
        lv=(ListView) view.findViewById(R.id.listView1);
        adapter=new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, movies);
        lv.setAdapter(adapter);
        lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                //通过流读取数据准换成字符串显示到rightfragment中的textview中
                ByteArrayOutputStream byteArrayOutputStream=null;
                try {
                    //点击某个item  找到assets文件夹下的moviesX文件 然后进行解析
                    //获取AssetsManger对象调用open()方法获取读取流
                    InputStream inputStream=getResources().getAssets().open("movie"+position+".txt");
                    byteArrayOutputStream = new ByteArrayOutputStream();
                    int temp=0;
                    byte[] buff=new byte[1024];
                    while((temp=inputStream.read(buff))!=-1){
                        byteArrayOutputStream.write(buff, 0, temp);
                        byteArrayOutputStream.flush();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
                String data=byteArrayOutputStream.toString();//将内存流数据转换成字符串
                //获取rightfragment中的textview并且设置值
                TextView tv=(TextView) getActivity().findViewById(R.id.textView1);
                tv.setText(data);
            }
        });
        return view;
    }

}
KnightBubble commented 9 years ago

主要知识点

fragment listview 和adapter listView 中的item监听 inputStrem 读取文件的io流

KnightBubble commented 9 years ago

获得一个ArrayAdapter

ArrayAdapter<String>  adapter = null;
adapter  = new ArrayAdapter(getActivity(),android.R.layout.simple_list_item_1, movieList);

第一个参数context => getActivity()获得activity的执行上下文 第二个参数 int标明样式 数据源 数组形式

KnightBubble commented 9 years ago

获取资源文件

getResources().getAssets().open("content0.txt");
KnightBubble commented 9 years ago

读取文件流

InputStream inputStream = getResources().getAssets().open("content" + position + ".txt");
                    byteArrayOutputStream = new ByteArrayOutputStream();
                    int temp=0;
                    byte[] buff=new byte[1024];
                    while((temp=inputStream.read(buff))!=-1){
                        byteArrayOutputStream.write(buff, 0, temp);
                        byteArrayOutputStream.flush();
                    }
                    String data=byteArrayOutputStream.toString();//将内存流数据转换成字符串