我想在使用标签菜单时使用不同的详细信息进行Listview项目。
这是我的应用程序屏幕更好理解
http://tinypic.com/r/2niskna/9我的列表视图
http://i68.tinypic.com/9laoaq.png和详情页面
我想在用户点击listview项目时显示不同的详细信息。布局设计相同,但文字细节和图像不同。
也许我的代码很复杂我不知道我是android的初学者,你只能告诉我逻辑然后我才能做到。
asker.java
package com.example.fb.clashdeneme;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
/**
* Created by FB on 19.10.2016.
*/
public class asker extends AppCompatActivity {
ListView list;
String [] askerbaslik;
int [] images={R.drawable.barbar_icon,R.drawable.okcu_icon,R.drawable.dev_icon,R.drawable.goblin_icon,R.drawable.duvar_kirici_icon,R.drawable.balon_icon,
R.drawable.buyucu_icon,R.drawable.sifaci_icon,R.drawable.ejderha_icon,R.drawable.pekka_icon,R.drawable.yavruejder_icon,R.drawable.madenci_icon};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview);
Resources res=getResources();
askerbaslik= res.getStringArray(R.array.asker_adlari);
list = (ListView) findViewById(R.id.listview);
ilyasadapter adapter=new ilyasadapter(this,askerbaslik,images);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch (position){
case 0:
Intent intent_barbar = new Intent(getApplicationContext(), Asker_detaylar.class);
intent_barbar.putExtra("Position", position);
startActivity(intent_barbar);
break;
case 1:
Intent intent_okcu = new Intent(getApplicationContext(), Asker_detaylar.class);
intent_okcu.putExtra("intVariableName", position);
startActivity(intent_okcu);
break;
case 2:
Intent intent_dev = new Intent(asker.this, Asker_detaylar.class);
intent_dev.putExtra("intVariableName", position);
startActivity(intent_dev);
break;
case 3:
Intent intent_goblin = new Intent(asker.this, Asker_detaylar.class);
intent_goblin.putExtra("intVariableName", position);
startActivity(intent_goblin);
break;
case 4:
Intent intent_duvarkirici = new Intent(asker.this, Asker_detaylar.class);
intent_duvarkirici.putExtra("intVariableName", position);
startActivity(intent_duvarkirici);
break;
case 5:
Intent intent_balon = new Intent(asker.this, Asker_detaylar.class);
intent_balon.putExtra("intVariableName", position);
startActivity(intent_balon);
break;
case 6:
Intent intent_buyucu = new Intent(asker.this, Asker_detaylar.class);
intent_buyucu.putExtra("intVariableName", position);
startActivity(intent_buyucu);
break;
case 7:
Intent intent_sifaci= new Intent(asker.this, Asker_detaylar.class);
intent_sifaci.putExtra("intVariableName", position);
startActivity(intent_sifaci);
break;
case 8:
Intent intent_ejder = new Intent(asker.this, Asker_detaylar.class);
intent_ejder.putExtra("intVariableName", position);
startActivity(intent_ejder);
break;
case 9:
Intent intent_pekka= new Intent(asker.this, Asker_detaylar.class);
intent_pekka.putExtra("intVariableName", position);
startActivity(intent_pekka);
break;
case 10:
Intent intent_yavruejder= new Intent(asker.this, Asker_detaylar.class);
intent_yavruejder.putExtra("intVariableName", position);
startActivity(intent_yavruejder);
break;
case 11:
Intent intent_madenci = new Intent(asker.this, Asker_detaylar.class);
intent_madenci.putExtra("intVariableName", position);
startActivity(intent_madenci);
break;
}
}
});
}
public class ilyasadapter extends ArrayAdapter<String> {
Context context;
int [] images;
String [] titlearray;
ilyasadapter (Context c,String[] titles, int imgs[]) {
super(c,R.layout.liste_tasarim,R.id.asker_baslik,titles);
this.context=c;
this.images=imgs;
this.titlearray=titles;
}
class MyViewHolder {
TextView mytitle;
ImageView myimage;
MyViewHolder(View v) {
mytitle=(TextView)v.findViewById(R.id.asker_baslik);
myimage=(ImageView)v.findViewById(R.id.asker_icon);
}
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row=convertView;
MyViewHolder holder=null;
if (row==null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.liste_tasarim, parent, false);
holder=new MyViewHolder(row);
row.setTag(holder);
Log.d("test","creating a new row");
}
else
{
holder =(MyViewHolder) row.getTag();
Log.d("test","Recycling stuff");
}
holder.myimage.setImageResource(images[position]);
holder.mytitle.setText(titlearray[position]);
return row;
}
}
}
&#13;
Asker_detaylar.java
package com.example.fb.clashdeneme;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
import android.content.Context;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
/**
* Created by FB on 20.10.2016.
*/
public class Asker_detaylar extends AppCompatActivity {
TabLayout tabLayout;
ViewPager viewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent mIntent = getIntent();
int intValue = mIntent.getIntExtra("intVariableName", 0);
switch (intValue) {
case 0:
setContentView(R.layout.detay_tasarim);
break;
case 1:
setContentView(R.layout.detay_tasarim);
// TextView mTextView2 = (TextView) findViewById(R.id.textView);
// mTextView2.setText(R.string.aciklama2);
break;
case 2:
setContentView(R.layout.detay_tasarim);
break;
case 3:
setContentView(R.layout.detay_tasarim);
break;
case 4:
setContentView(R.layout.detay_tasarim);
break;
case 5:
setContentView(R.layout.detay_tasarim);
break;
case 6:
setContentView(R.layout.detay_tasarim);
break;
case 7:
setContentView(R.layout.detay_tasarim);
break;
case 8:
setContentView(R.layout.detay_tasarim);
break;
case 9:
setContentView(R.layout.detay_tasarim);
break;
case 10:
setContentView(R.layout.detay_tasarim);
break;
case 11:
setContentView(R.layout.detay_tasarim);
break;
}
viewPager = (ViewPager) findViewById(R.id.viewPager);
viewPager.setAdapter(new CustomAdapter(getSupportFragmentManager(), getApplicationContext()));
tabLayout = (TabLayout) findViewById(R.id.tabLayout);
tabLayout.setupWithViewPager(viewPager);
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
});
}
private class CustomAdapter extends FragmentPagerAdapter {
private String fragments [] = {"Genel Özellikler","Saldırı","Savunma","Yükseltme"};
public CustomAdapter(FragmentManager supportFragmentManager, Context applicationContext) {
super(supportFragmentManager);
}
@Override
public Fragment getItem(int position) {
switch (position){
case 0:
return new Genel_tab();
case 1:
return new Saldiri_tab();
case 2:
return new Savunma_tab();
case 3:
return new Yukseltme_tab();
default:
return null;
}
}
@Override
public int getCount() {
return fragments.length;
}
@Override
public CharSequence getPageTitle(int position) {
return fragments[position];
}
}
}
&#13;
Genel_tab.java
package com.example.fb.clashdeneme;
/**
* Created by FB on 22.10.2016.
*/
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
/**
* Created by filip on 8/21/2015.
*/
public class Genel_tab extends Fragment {
int pos=0;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.genel_tab_layout, container, false);
final String [] isimlerarray;
final String ozellikler1,ozellikler2,ozellikler3,ozellikler4,ozellikler5;
final int [] resimler={R.drawable.barbarian1and2,R.drawable.barbarian1and2,R.drawable.barbarian3and4,R.drawable.barbarian3and4,R.drawable.barbarian5,R.drawable.barbarian6,
R.drawable.barbarian7};
Button ileri = (Button)view.findViewById(R.id.ileri_btn);
Button geri = (Button)view.findViewById(R.id.geri_btn);
Resources res = getResources();
isimlerarray = res.getStringArray(R.array.asker_anabaslik);
ozellikler1 = res.getString(R.string.barbarozellikleri1);
ozellikler2 = res.getString(R.string.barbarozellikleri2);
ozellikler3 = res.getString(R.string.barbarozellikleri3);
ozellikler4 = res.getString(R.string.barbarozellikleri4);
ozellikler5 = res.getString(R.string.barbarozellikleri5);
// get views
final ImageView img = (ImageView)view.findViewById(R.id.askerdetay_icon);
final TextView txtanabaslik = (TextView)view.findViewById(R.id.anatextview);
final TextView fvcvp = (TextView)view.findViewById(R.id.favoricvp);
final TextView hsrcvp = (TextView)view.findViewById(R.id.hasarcvp);
final TextView alan = (TextView) view.findViewById(R.id.barinmacvp);
final TextView hiz = (TextView) view.findViewById(R.id.hizcvp);
final TextView egitim = (TextView)view.findViewById(R.id.egitimcvp);
// set data
img.setImageResource(resimler[pos]);
txtanabaslik.setText(isimlerarray[pos]);
fvcvp.setText(ozellikler1);
hsrcvp.setText(ozellikler2);
alan.setText(ozellikler3);
hiz.setText(ozellikler4);
egitim.setText(ozellikler5);
ileri.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int position = pos + 1;
if (position < resimler.length) {
img.setImageResource(resimler[position]);
txtanabaslik.setText(isimlerarray[position]);
fvcvp.setText(ozellikler1);
hsrcvp.setText(ozellikler2);
alan.setText(ozellikler3);
hiz.setText(ozellikler4);
egitim.setText(ozellikler5);
pos += 1;
}
}
});
geri.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int position = pos - 1;
if (!(position < 0)) {
img.setImageResource(resimler[position]);
txtanabaslik.setText(isimlerarray[position]);
fvcvp.setText(ozellikler1);
hsrcvp.setText(ozellikler2);
alan.setText(ozellikler3);
hiz.setText(ozellikler4);
egitim.setText(ozellikler5);
pos -= 1;
}
}
});
return view;
}
}
&#13;
genel_tab_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#607D8B">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" "
android:layout_marginTop="5dp"
android:id="@+id/anatextview"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="GERİ"
android:layout_gravity="center"
android:layout_weight="1"
android:id="@+id/geri_btn"
/>
<ImageView
android:layout_width="500px"
android:layout_height="500px"
android:layout_alignParentTop="true"
android:layout_marginTop="20dp"
android:layout_gravity="center"
android:id="@+id/askerdetay_icon"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="İLERİ"
android:layout_gravity="center"
android:layout_weight="1"
android:id="@+id/ileri_btn"
/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Favori Hedef:"
android:id="@+id/TextFavori"
android:layout_marginLeft="10dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hasar Türü:"
android:layout_below="@+id/TextFavori"
android:layout_marginTop="5dp"
android:id="@+id/TextHasar"
android:layout_marginLeft="10dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Barınma Alanı:"
android:layout_below="@+id/TextHasar"
android:layout_marginTop="5dp"
android:id="@+id/TextBarinma"
android:layout_marginLeft="10dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hareket Hızı:"
android:layout_below="@+id/TextBarinma"
android:layout_marginTop="5dp"
android:id="@+id/TextHiz"
android:layout_marginLeft="10dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Eğitim Süresi:"
android:layout_below="@+id/TextHiz"
android:layout_marginTop="5dp"
android:id="@+id/TextEgitimSuresi"
android:layout_marginLeft="10dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/favoricvp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/hasarcvp"
android:layout_marginTop="5dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/barinmacvp"
android:layout_marginTop="5dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/hizcvp"
android:layout_marginTop="5dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/egitimcvp"
android:layout_marginTop="5dp"
/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Genel Özellikleri"
android:textStyle="bold"
android:textSize="20dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="yazılar"
/>
</LinearLayout>
</LinearLayout>
&#13;