У меня есть макет с ImageSwitcher
, галереей Thumnail и TextSwitcher
.Я бы хотел, чтобы TextSwitcher
обновлял текст в зависимости от выбранного эскиза.Я пытался использовать baseadapter для обновления TextSwitcher
аналогично тому, как ImageSwitcher
обновляет изображения в ImageView
, но я сталкиваюсь здесь со стеной.Любая помощь приветствуется!Спасибо!
JAVA:
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.TextSwitcher;
import android.widget.TextView;
import android.widget.ViewSwitcher;
import android.widget.Gallery.LayoutParams;
public class ImageSwitch1 extends Activity implements
AdapterView.OnItemSelectedListener, ViewSwitcher.ViewFactory {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.imageswitcher);
TextSwitcher mTextSwitcher = (TextSwitcher) findViewById(R.id.textSwitcher1);
mSwitcher = (ImageSwitcher) findViewById(R.id.switcher);
mSwitcher.setFactory(this);
mSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_in));
mSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_out));
mTextSwitcher.setFactory(this);
mTextSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_in));
mTextSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_out));
Gallery g = (Gallery) findViewById(R.id.gallery);
g.setAdapter(new ImageAdapter(this));
g.setOnItemSelectedListener(this);
}
public void onNothingSelected(AdapterView parent) {
}
public View makeView() {
ImageView i = new ImageView(this);
i.setBackgroundColor(0xFF000000);
i.setScaleType(ImageView.ScaleType.FIT_CENTER);
i.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
return i;
}
private TextSwitcher mTextSwitcher;
public class TextAdapter extends BaseAdapter{
public TextAdapter(Context c){
mContext = c;
}
public int getCount(){
return mThumbIds.length;
}
public Object getItem(int position){
return position;
}
public long getItemId(int position){
return position;
}
public View getView(int position, View convertView, ViewGroup parent){
TextView t = new TextView(mContext);
t.setText(mText[position]);
t.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL);
t.setTextSize(36);
return t;
}
private Context mContext;
}
private ImageSwitcher mSwitcher;
public class ImageAdapter extends BaseAdapter {
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView i = new ImageView(mContext);
i.setImageResource(mThumbIds[position]);
i.setAdjustViewBounds(true);
i.setLayoutParams(new Gallery.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
i.setBackgroundResource(R.drawable.picture_frame);
return i;
}
private Context mContext;
}
public void onItemSelected(AdapterView parent, View v, int position, long id) {
mSwitcher.setImageResource(mImageIds[position]);
mTextSwitcher.setText(mThumbIds[position]);
}
private Integer[] mThumbIds = {
R.drawable.image1_thumb, R.drawable.image2_thumb, R.drawable.image3_thumb,
R.drawable.image4_thumb, R.drawable.image5_thumb};
private Integer[] mImageIds = {
R.drawable.image1, R.drawable.image2, R.drawable.image3,
R.drawable.image4, R.drawable.image5};
private Integer[] mText = {
R.string.item1, R.string.item2, R.string.item3, R.string.item4, R.string.item5
};
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout android:id="@+id/FrameLayout1" android:layout_height="match_parent" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent">
<ImageSwitcher android:id="@+id/switcher"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
</ImageSwitcher>
<Gallery android:id="@+id/gallery"
android:background="#55000000"
android:layout_width="match_parent"
android:gravity="center_vertical"
android:spacing="16dp" android:unselectedAlpha="0.5" android:layout_height="80dp" />
<TextSwitcher android:layout_width="match_parent" android:id="@+id/textSwitcher1" android:layout_height="wrap_content"></TextSwitcher>
</FrameLayout>