景区介绍界面(Android)

来源:互联网 发布:淘宝联盟为啥登不上去 编辑:程序博客网 时间:2024/05/08 10:55

描述:自定义listview控件,通过适配器来设置数据和图片,读取txt文件来获取景点介绍信息。

activity_main.xml文件代码:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/activity_main"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="#aabbcc"    android:orientation="vertical"    tools:context="scenicspot.com.scenicspot.MainActivity">    <TextView        android:background="#ccbbaa"        android:textColor="#000000"        android:padding="10dp"        android:gravity="center"        android:text="@string/china_spot"        android:textSize="24sp"        android:layout_width="match_parent"        android:layout_height="wrap_content" />    <ListView        android:id="@+id/scenery"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:divider="#aaaaaa"        android:dividerHeight="2dp"        android:gravity="center">    </ListView></LinearLayout>
item.xml文件代码:

<?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">    <ImageView        android:id="@+id/image"        android:layout_width="100dp"        android:layout_height="75dp"        android:contentDescription="@string/imgText"/>    <LinearLayout        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_margin="10dp"        android:orientation="vertical">        <TextView            android:id="@+id/name"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:textColor="#000000"            android:textSize="20sp"/>        <TextView            android:id="@+id/brief"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_marginTop="10dp"            android:gravity="left"            android:singleLine="true"            android:ellipsize="end"            android:textColor="#0000ee"            android:textSize="12sp"/>    </LinearLayout></LinearLayout>

scenery_show.xml文件代码:

<?xml version="1.0" encoding="utf-8"?><ScrollView xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent">    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:background="#aabbcc"        android:orientation="vertical">        <TextView            android:id="@+id/title"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:gravity="center"            android:padding="10dp"            android:background="#ccbbaa"            android:textSize="24sp"/>        <ImageView            android:id="@+id/image"            android:layout_width="320dp"            android:layout_height="240dp"            android:scaleType="fitXY"            android:contentDescription="@string/imgInfo"/>        <TextView            android:id="@+id/content"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:background="#000000"            android:textColor="#00ff00"            android:textSize="16sp"            android:padding="10dp"/>    </LinearLayout></ScrollView>
MainActivity.java文件代码:
package scenicspot.com.scenicspot;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.widget.AdapterView;import android.widget.ListView;import android.widget.SimpleAdapter;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import java.util.Objects;public class MainActivity extends Activity {    private ListView lv_scenery;    private List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();    private String[] names = new String[]{            "武功山","明月山","三清山","庐山","武当山","终南山","龙虎山"    };    private String[] briefs = new String[]{"武功山位于中国江西省中西部,居罗霄山脉北支," +            "山体呈东北——西南走向,地跨江西萍乡市芦溪县、吉安市安福县,宜春市袁州区," +            "主脉绵延120余千米,总面积约970平方千米。"            ,"明月山旅游区位于赣西中部有“中国宜居城市”、“国家园林城市”、“国家卫生城市”" +            "、“国家绿化模范城市”、“2010中国最佳休闲养生城市”之称的宜春市中心城西南15公里处," +            "旅游区面积104平方公里。","三清山又名少华山、丫山," +            "位于中国江西省上饶市玉山县与德兴市交界处。" +            "因玉京、玉虚、玉华三峰宛如道教玉清、上清、太清三位尊神列坐山巅而得名。其中玉京峰为最高," +            "海拔1819.9米,是江西第五高峰和怀玉山脉的最高峰,也是信江的源头。三清山是道教名山," +            "世界自然遗产地、世界地质公园、国家自然遗产、国家地质公园。","庐山[1]  ," +            "又名匡山、匡庐,是中华十大名山、世界文化遗产、" +            "国家AAAAA级旅游景区、中国四大避暑胜地。地处江西省九江市庐山市境内," +            "山体呈椭圆形,典型的地垒式块段山。","武当山,道教圣地," +            "位于湖北省十堰市境内。武当山又名太和山、谢罗山、参上山、仙室山," +            "古有“太岳”、“玄岳”、“大岳”之称。","终南山又" +            "名太乙山、地肺山、中南山、周南山,简称南山,位于秦岭山脉中段,是中国重要的地理标志," +            "道教的发祥圣地。","龙虎山,位于江西省鹰潭市西南20公里处。" +            "东汉中叶,正一道创始人张道陵曾在此炼丹,传说“丹成而龙虎现,山因得名。" +            "其中天门山最高,海拔1300米。龙虎山是中国第八处世界自然遗产[1]  ,世界地质公园[2]  、" +            "国家自然文化双遗产地[3]  、国家AAAAA级旅游景区[4]  、全国重点文物保护单位。"    };    private int[] imgIds = new int[]{R.drawable.wugong,R.drawable.mingyue,R.drawable.sanqing,            R.drawable.lushan,R.drawable.wudang,R.drawable.zhongnan,R.drawable.longhu};    private int[] contentIds = new int[]{R.raw.wudang_into,R.raw.mingyue_into,R.raw.sanqing_into,            R.raw.lushan_into,R.raw.wudang_into,R.raw.zhongnan_into,R.raw.longhu_into};    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        lv_scenery = (ListView) findViewById(R.id.scenery);        init();        SimpleAdapter adapter = new SimpleAdapter(this, list, R.layout.item,                new String[]{"img", "name", "brief"},                new int[]{R.id.image, R.id.name, R.id.brief});        lv_scenery.setAdapter(adapter);        lv_scenery.setOnItemClickListener(new AdapterView.OnItemClickListener() {            @Override            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {                Intent intent = new Intent();                intent.setClass(MainActivity.this,SceneryShowActivity.class);                intent.putExtra("name",(String) list.get(position).get("name"));                intent.putExtra("image",(Integer) list.get(position).get("img"));                intent.putExtra("content",(Integer) list.get(position).get("content"));                startActivity(intent);            }        });    }    private void init() {        for (int i =0;i < imgIds.length;i++){            Map<String,Object>item = new HashMap<String, Object>();            item.put("img",imgIds[i]);            item.put("name",names[i]);            item.put("brief",briefs[i]);            item.put("content",contentIds[i]);            list.add(item);        }    }}

SceneryShowActivity.java文件代码:
package scenicspot.com.scenicspot;import android.app.Activity;import android.graphics.drawable.ClipDrawable;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.view.Gravity;import android.widget.ImageView;import android.widget.TextView;import java.io.IOException;import java.io.InputStream;/** * Created by Administrator on 2017/4/19 0019. */public class SceneryShowActivity extends Activity{    private ImageView imageView;    private TextView content;    private TextView title;    private ClipDrawable clipDrawable;    private Handler mHandler;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.scenery_show);        imageView = (ImageView) findViewById(R.id.image);        content = (TextView) findViewById(R.id.content);        title = (TextView) findViewById(R.id.title);        String titleStr = getIntent().getStringExtra("name");        int image = getIntent().getIntExtra("image",R.drawable.wugong);        int info = getIntent().getIntExtra("content",R.drawable.wugong1);        title.setText(titleStr);        clipDrawable = new ClipDrawable(getResources().getDrawable(image), Gravity.CENTER, ClipDrawable.HORIZONTAL);        imageView.setImageDrawable(clipDrawable);        startThread();        InputStream inputStream = getResources().openRawResource(info);        content.setText(getStringFromInputStream(inputStream));        mHandler = new Handler() {            @Override            public void handleMessage(Message msg) {                clipDrawable.setLevel(clipDrawable.getLevel() + 400);            }        };    }    public String getStringFromInputStream(InputStream inputStream) {        byte[] buffer = new byte[1024];        int hasRead = 0;        StringBuilder result = null;        result = new StringBuilder("");        try {            while ((hasRead = inputStream.read(buffer)) != -1){                result.append(new String(buffer,0,hasRead,"GBK"));            }        } catch (Exception e) {            e.printStackTrace();        }        return result.toString();    }    private void startThread() {        clipDrawable.setLevel(0);        new Thread(){            @Override            public void run() {               while (clipDrawable.getLevel() < 10000){                   try {                       Thread.sleep(300);                       mHandler.sendEmptyMessage(0x11);                   } catch (InterruptedException e) {                       e.printStackTrace();                   }               }            }        }.start();    }}

raw文件截图:
运行效果图:

原创粉丝点击