最近项目需要,在搞android.开发环境:android studio
越用越发现这软件不错,虽然我还不太会使用这软件
软件的使用教程网上一堆,记录一下左右滑动切换界面,
需要切换的几个界面如下:
思路是这样的:四个xml布局文件,四个activity,一个布局文件对应一个activity,
当然点击按钮也是可以切换的,四个按钮都是差不多的,红色部分一替换就好,这个代码可以参考如下:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i(TAG, "onCreate called.");
setContentView(R.layout.layout);
Button btn = (Button) findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(<span style="color:#ff0000;">xxx.this,***.class</span>);
想要实现左右滑动就切换界面,activity 需要实现两个接口implements View.OnTouchListener,GestureDetector.OnGestureListener
重写里面的方法。四个activity 都是一样的,我贴出其中一个ResultActivity
<pre name="code" class="java">package android.cl.com.myapplication2;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;
import java.util.ArrayList;
import java.util.List;
public class ResultActivity extends AppCompatActivity implements View.OnTouchListener,GestureDetector.OnGestureListener {
private RelativeLayout rl;
private GestureDetector gd;
private ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test_result);
rl=(RelativeLayout)findViewById(R.id.resultRelative);
rl.setOnTouchListener(this);
rl.setLongClickable(true); //很重要
gd=new GestureDetector((GestureDetector.OnGestureListener)this);
//我们设置一个List集合,然后向里边添加几条数据
List<String> ls = new ArrayList<String>();
ls.add("结果1");
ls.add("结果2");
ls.add("结果3");
//获取xml文件中listView控件
listView = (ListView)findViewById(R.id.resultListView);
//然后为listView控件调用setAdapter方法,让数据显示在界面上。
listView.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,ls));
}
@Override
public boolean onDown(MotionEvent e) {
return false;
}
@Override
public void onShowPress(MotionEvent e) {
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
return false;
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
return false;
}
@Override
public void onLongPress(MotionEvent e) {
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
final int FLING_MIN_DISTANCE=100;
final int FLING_MIN_VELOCITY=200;
//左
if(e1.getX() - e2.getX() > FLING_MIN_DISTANCE && Math.abs(velocityX) > FLING_MIN_VELOCITY){
Intent intent = new Intent(ResultActivity.this,MessageActivity.class);
startActivity(intent);
}
//右
if(e1.getX() - e2.getX() < FLING_MIN_DISTANCE && Math.abs(velocityX) < FLING_MIN_VELOCITY){
Intent intent = new Intent(ResultActivity.this,TestActivity.class);
startActivity(intent);
}
return false;
}
@Override
public boolean onTouch(View v, MotionEvent event) {
return gd.onTouchEvent(event);
}
}
这里用到了listView显示,布局文件test_result:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1"
android:id="@+id/resultRelative">
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="测试"
android:id="@+id/button2"
android:layout_gravity="left|bottom"
android:layout_alignParentBottom="true"
android:background="#C4C4C4"
android:layout_alignTop="@+id/button3" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="结果"
android:id="@+id/button3"
android:layout_gravity="center_horizontal|bottom"
android:background="#949494"
android:layout_alignParentBottom="true"
android:layout_toRightOf="@+id/button2"
android:layout_toEndOf="@+id/button2"
android:layout_marginLeft="69dp"
android:layout_marginStart="69dp" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="信息"
android:id="@+id/button4"
android:background="#C4C4C4"
android:layout_alignParentBottom="true"
android:layout_toLeftOf="@+id/button5"
android:layout_toStartOf="@+id/button5"
android:layout_marginRight="53dp"
android:layout_marginEnd="53dp" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="配置"
android:id="@+id/button5"
android:background="#C4C4C4"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/resultListView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_above="@+id/button2" />
</RelativeLayout>
上面的只能显示单一的一条记录,使用
SimpleAdapter
可以显示多种元素,比如图片啦等等,看一下代码,其他的重写代码参考上面的
public class MessageActivity extends AppCompatActivity implements View.OnTouchListener,GestureDetector.OnGestureListener {
private RelativeLayout rl;
private GestureDetector gd;
private String[] names = new String[]
{ "叮当猫", "海贼王", "樱桃小丸子", "熊猫"};
private String[] descs = new String[]
{ "可爱的小孩", "One pease"
, "一个Q女性", "国宝动物"};
//这是三张图片的id的集合
private int[] imageIds = new int[]
{ R.drawable.dingdangmao , R.drawable.haizeiwang
, R.drawable.yingtaoxiaowanzi, R.drawable.xiongmao};
private ListView messagelistview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test_message);
rl=(RelativeLayout)findViewById(R.id.messageRelative);
rl.setOnTouchListener(this);
rl.setLongClickable(true); //很重要
gd=new GestureDetector((GestureDetector.OnGestureListener)this);
ArrayList<Map<String, Object>> listems = new ArrayList<Map<String, Object>>();
for (int i = 0; i < names.length; i++) {
Map<String, Object> listem = new HashMap<String, Object>();
listem.put("head", imageIds[i]);
listem.put("name", names[i]);
listem.put("desc", descs[i]);
listems.add(listem);
}
/*SimpleAdapter的参数说明
* 第一个参数 表示访问整个android应用程序接口,基本上所有的组件都需要
* 第二个参数表示生成一个Map(String ,Object)列表选项
* 第三个参数表示界面布局的id 表示该文件作为列表项的组件
* 第四个参数表示该Map对象的哪些key对应value来生成列表项
* 第五个参数表示来填充的组件 Map对象key对应的资源一依次填充组件 顺序有对应关系
* 注意的是map对象可以key可以找不到 但组件的必须要有资源填充 因为 找不到key也会返回null 其实就相当于给了一个null资源
* 下面的程序中如果 new String[] { "name", "head", "desc","name" } new int[] {R.id.name,R.id.head,R.id.desc,R.id.head}
* 这个head的组件会被name资源覆盖
* */
SimpleAdapter simplead = new SimpleAdapter(this, listems,
R.layout.messagelistview, new String[] { "name", "head", "desc" },
new int[] {R.id.name,R.id.head,R.id.desc});
messagelistview=(ListView)findViewById(R.id.messageListView);
messagelistview.setAdapter(simplead);
messagelistview.<span style="color:#ff0000;">setOnItemClickListene</span>r(new AdapterView.OnItemClickListener(){
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
//通过单击事件,获得单击选项的内容
String text = messagelistview.getItemAtPosition(arg2)+"";
//通过吐丝对象显示出来。
Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT).show();
}
});
}
写了一个Item的点击监听事件,为了以后可以显示其他的界面,这里只是输出信息
<?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/head"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:textColor="#f0f"
android:paddingLeft="10dp"/>
<TextView
android:id="@+id/desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14dp"
android:paddingLeft="10dp"/>
</LinearLayout>
</LinearLayout>
OK 完事啦。