上次我们说到了对listview控件内容的控制,这次我要实现的是列表的点击事件,就是点击后会跳转到其他页面,这就类似于你看今日头条,找到一个喜欢的内容然后点进去出现界面。基本上没有大的变动,我还是把所有代码附上
这个新增加的MainActivity2Activity就是实现跳转后的页面的,对应的xml也是跳转后页面布局内容
MainActivity2Activity:
package com.example.lenovo.myapplication;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity2Activity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity2);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main_activity2, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
对应xml:
<RelativeLayout 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" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.example.lenovo.myapplication.MainActivity2Activity">
<TextView android:text="@string/hello_world" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
Adpator:
package com.example.lenovo.myapplication;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.ArrayList;
/**
* Created by lenovo on 2021/10/18.
*/
public class Adpator extends BaseAdapter{
Activity aact;
ArrayList<News> mmlist=null;
public Adpator(Activity act,ArrayList<News> list){
this.aact=act;
this.mmlist=list;
}
@Override
public int getCount() {
return mmlist.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater=aact.getLayoutInflater();
LinearLayout item_view=(LinearLayout)inflater.inflate(R.layout.list,null);
TextView text1=(TextView)item_view.findViewById(R.id.text1);
TextView text2=(TextView)item_view.findViewById(R.id.text2);
text1.setText(mmlist.get(position).title);
text2.setText(mmlist.get(position).content);
return item_view;
}
}
对应xml list:
<?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="150dp">
<LinearLayout
android:layout_width="150dp"
android:layout_height="150dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/bg_catagories_def"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="okokokoko"
android:id="@+id/text1"
android:textColor="#fe20"
android:textSize="30sp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="opopopopo"
android:id="@+id/text2"
android:textColor="#ff31ee9b"
android:textSize="30sp"/>
</LinearLayout>
</LinearLayout>
Mainactivity:
package com.example.lenovo.myapplication;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import java.util.ArrayList;
public class MainActivity extends Activity {
ArrayList<News> m_list=new ArrayList<News>();
private void initdata(){
m_list.clear();
News n1=new News();
n1.title="先获得随机获得随机";
n1.content="还记得上飞机";
m_list.add(n1);
News n2=new News();
n2.title="先获得随机获得随机";
n2.content="还记得上飞机";
m_list.add(n2);
News n3=new News();
n3.title="先获得随机获得随机";
n3.content="还记得上飞机";
m_list.add(n3);
News n4=new News();
n4.title="先获得随机获得随机";
n4.content="还记得上飞机";
m_list.add(n4);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView=(ListView)findViewById(R.id.list);
initdata();
Adpator adpator=new Adpator(this,m_list);
listView.setAdapter(adpator);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if(position==0){
show();
}
}
});
}
private void show(){
Intent i=new Intent(this,MainActivity2Activity.class);
startActivity(i);
}
}
对应xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/list">
</ListView>
</LinearLayout>
</LinearLayout>
News:
package com.example.lenovo.myapplication;
/**
* Created by lenovo on 2021/10/18.
*/
public class News {
public int id=0;
String title="";
String content="";
}
运行:
点击第一个后跳转