Java ListView和图像的实现
我的问题是,我想用列表和图像实现一个活动,在图像的底部有四个文本。当我要打开我的应用程序时,它必须显示一个选定的文本,其相关图像和列表为默认值。当单击其余文本时,它必须在同一活动中分别显示它们的相关列表和图像。因为我是android新手请帮帮我 下面是Java ListView和图像的实现,java,android,listview,android-fragments,Java,Android,Listview,Android Fragments,我的问题是,我想用列表和图像实现一个活动,在图像的底部有四个文本。当我要打开我的应用程序时,它必须显示一个选定的文本,其相关图像和列表为默认值。当单击其余文本时,它必须在同一活动中分别显示它们的相关列表和图像。因为我是android新手请帮帮我 下面是xmlcode <RelativeLayout android:id="@+id/main" android:layout_width="match_parent" android:layout_height="mat
xml
code
<RelativeLayout
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="2.5"
>
<ImageView
android:id="@+id/mainimage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/pestf"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="20dp"
android:orientation="horizontal"
android:layout_gravity="bottom"
android:layout_alignParentBottom="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pest Control"
android:id="@+id/pestf"
android:onClick="click1"
android:clickable="true"
android:textStyle="normal"
android:textSize="15dp"
android:textColor="#cf0c07"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/packf"
android:layout_marginLeft="5dp"
android:id="@+id/packf"
android:onClick="click1"
android:clickable="true"
android:textStyle="normal"
android:textSize="15dp"
android:textColor="#cf0c07"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cleaning"
android:layout_marginLeft="5dp"
android:id="@+id/cleanf"
android:onClick="click1"
android:clickable="true"
android:textStyle="normal"
android:textSize="15dp"
android:textColor="#cf0c07"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Landscaping"
android:id="@+id/landf"
android:onClick="click1"
android:clickable="true"
android:layout_marginLeft="5dp"
android:textStyle="normal"
android:textSize="15dp"
android:textColor="#cf0c07"/>
</LinearLayout>
</RelativeLayout>
<ListView
android:id="@+id/lst"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/ll_pest"
android:layout_weight="2.2"
android:background="#FFFFFF"
>
</ListView>
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/lst"
android:layout_weight="3.5"
android:gravity="center"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:layout_marginTop="3dp"
android:layout_alignParentBottom="true"
android:background="@drawable/border_layout">
<TextView
android:id="@+id/txt_pest_Email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:background="#FFFFFF"
android:text="@string/btn7"
android:drawablePadding="5dp"
android:drawableTop="@drawable/icon7"
android:textStyle="bold"
android:textColor="#000000"
/>
<View
android:layout_width="1dp"
android:layout_height="fill_parent"
android:background="#82020202" />
<TextView
android:id="@+id/txt_pest_phone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:background="#ffffff"
android:text="@string/btn8"
android:drawablePadding="5dp"
android:drawableTop="@drawable/icon8"
android:textStyle="bold"
android:textColor="#000000"
/>
</LinearLayout>
这是活动代码
public class demo2 extends AppCompatActivity {
TextView tv1,tv2,tv3,tv4,txtEmail,txtPhone;
ListView lv;
ImageView img;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_demo2);
String[] adobe_products = getResources().getStringArray(R.array.adobe_products);
lv = (ListView) findViewById(R.id.lst);
img = (ImageView) findViewById(R.id.mainimage);
txtEmail=(TextView)findViewById(R.id.txt_pest_Email);
txtPhone=(TextView)findViewById(R.id.txt_pest_phone);
tv1 = (TextView) findViewById(R.id.text1);
tv2 = (TextView) findViewById(R.id.text2);
tv3 = (TextView) findViewById(R.id.text3);
tv4 = (TextView) findViewById(R.id.text4);
lv.setAdapter(new ArrayAdapter<String>(this, R.layout.list_item, adobe_products));
tv1.setTextColor(getResources().getColor(R.color.blue));
txtPhone.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String mobileNo = "08042589999";
String uri = "tel:" + mobileNo.trim();
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse(uri));
startActivity(intent);
}
});
txtEmail.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(demo2.this, Booknow.class);
// sending data to new activity
i.putExtra("flag",true);
i.putExtra("cat",1);
startActivity(i);
}
});
// Inflate the layout for this fragment
}
public void click2(View v){
img.setBackgroundResource(R.drawable.packers);
String[] countries = getResources().getStringArray(R.array.country);
lv.setAdapter(new ArrayAdapter<String>(this, R.layout.list_item1, countries));
tv2.setTextColor(getResources().getColor(R.color.blue));
tv1.setTextColor(getResources().getColor(R.color.red));
tv3.setTextColor(getResources().getColor(R.color.red));
tv4.setTextColor(getResources().getColor(R.color.red));
}
public void click1(View v){
img.setBackgroundResource(R.drawable.pestf);
String[] adobe = getResources().getStringArray(R.array.adobe_products);
lv.setAdapter(new ArrayAdapter<String>(this, R.layout.list_item1, adobe));
tv1.setTextColor(getResources().getColor(R.color.blue));
tv2.setTextColor(getResources().getColor(R.color.red));
tv3.setTextColor(getResources().getColor(R.color.red));
tv4.setTextColor(getResources().getColor(R.color.red));
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// Launching new Activity on selecting single List Item
Intent i = new Intent(demo2.this, BedbugControl.class);
// sending data to new activity
i.putExtra("position", position);
startActivity(i);
}
});
}
public void click3(View v){
img.setBackgroundResource(R.drawable.cleaningf);
String[] sweets = getResources().getStringArray(R.array.sweets);
lv.setAdapter(new ArrayAdapter<String>(this, R.layout.list_item1, sweets));
tv3.setTextColor(getResources().getColor(R.color.blue));
tv1.setTextColor(getResources().getColor(R.color.red));
tv2.setTextColor(getResources().getColor(R.color.red));
tv4.setTextColor(getResources().getColor(R.color.red));
}
public void click4(View v) {
img.setBackgroundResource(R.drawable.landf);
String[] companies = getResources().getStringArray(R.array.company);
lv.setAdapter(new ArrayAdapter<String>(this, R.layout.list_item1, companies));
tv4.setTextColor(getResources().getColor(R.color.blue));
tv1.setTextColor(getResources().getColor(R.color.red));
tv2.setTextColor(getResources().getColor(R.color.red));
tv3.setTextColor(getResources().getColor(R.color.red));
}
@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_demo2, 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);
}
}
公共类demo2扩展了AppCompative活动{
TextView tv1、tv2、tv3、tv4、txtmail、txtPhone;
ListView lv;
图像视图img;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_demo2);
String[]adobe\u products=getResources().getStringArray(R.array.adobe\u products);
lv=(ListView)findViewById(R.id.lst);
img=(ImageView)findViewById(R.id.mainimage);
txtEmail=(TextView)findViewById(R.id.txt\u pest\u Email);
txtPhone=(TextView)findViewById(R.id.txt\u pest\u phone);
tv1=(TextView)findViewById(R.id.text1);
tv2=(TextView)findViewById(R.id.text2);
tv3=(TextView)findViewById(R.id.text3);
tv4=(TextView)findViewById(R.id.text4);
lv.setAdapter(新的ArrayAdapter(此,R.layout.list_项,adobe_产品));
tv1.setTextColor(getResources().getColor(R.color.blue));
setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
字符串mobileNo=“08042589999”;
String uri=“tel:”+mobileNo.trim();
意向意向=新意向(意向.行动\调用);
setData(Uri.parse(Uri));
星触觉(意向);
}
});
setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
意图i=新意图(demo2.this,Booknow.class);
//向新活动发送数据
i、 putExtra(“标志”,真);
i、 putExtra(“cat”,1);
星触觉(i);
}
});
//为该碎片膨胀布局
}
公共作废点击2(视图五){
img.立根资源(R.可抽出包装机);
String[]countries=getResources().getStringArray(R.array.country);
lv.setAdapter(新阵列适配器(此,R.layout.list_item1,国家/地区));
setextcolor(getResources().getColor(R.color.blue));
tv1.setTextColor(getResources().getColor(R.color.red));
setextcolor(getResources().getColor(R.color.red));
setextcolor(getResources().getColor(R.color.red));
}
公共作废点击1(视图五){
img.setBackgroundResource(R.drawable.pestf);
String[]adobe=getResources().getStringArray(R.array.adobe_products);
lv.setAdapter(新的ArrayAdapter(this,R.layout.list_item1,adobe));
tv1.setTextColor(getResources().getColor(R.color.blue));
setextcolor(getResources().getColor(R.color.red));
setextcolor(getResources().getColor(R.color.red));
setextcolor(getResources().getColor(R.color.red));
lv.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
public void onItemClick(AdapterView父级、视图、,
内部位置,长id){
//在选择单个列表项时启动新活动
Intent i=新Intent(demo2.this,BedbugControl.class);
//向新活动发送数据
i、 putExtra(“位置”,位置);
星触觉(i);
}
});
}
公共作废点击3(视图五){
img.退根资源(R.可拉拔.清洁F);
String[]sweets=getResources().getStringArray(R.array.sweets);
lv.setAdapter(新阵列适配器(此,R.layout.list_item1,sweets));
setextcolor(getResources().getColor(R.color.blue));
tv1.setTextColor(getResources().getColor(R.color.red));
setextcolor(getResources().getColor(R.color.red));
setextcolor(getResources().getColor(R.color.red));
}
公共作废点击4(视图五){
退耕地资源(R.drawable.land);
String[]companys=getResources().getStringArray(R.array.company);
lv.setAdapter(新阵列适配器(本,R.layout.list_item1,公司));
setextcolor(getResources().getColor(R.color.blue));
tv1.setTextColor(getResources().getColor(R.color.red));
setextcolor(getResources().getColor(R.color.red));
setextcolor(getResources().getColor(R.color.red));
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
//为菜单充气;这会将项目添加到操作栏(如果存在)。
getMenuInflater().充气(R.menu.menu\u demo2,menu);
返回true;
}
@凌驾
公共布尔值onOptionsItemSelected(菜单项项){
//处理操作栏项目单击此处。操作栏将
//自动处理Home/Up按钮上的点击,只要
//在AndroidManifest.xml中指定父活动时。
int id=item.getItemId();
//noinspection SimplifiableIf语句
if(id==R.id.action\u设置){
返回true;
}
返回super.onOptionsItemSelected(项目);
}
}
我建议不要再使用ListView,而是使用recyclerviews,它鼓励您使用自己的自定义布局,并在视图操作上提供了很大的自由度。我建议不要再使用ListView,而是使用recyclerviews,它鼓励您使用自己的自定义布局,并在视图操作上提供了很大的自由度