嗨,我在我的應用程序中創建了一個旋轉木馬,試圖在它下面添加文本視圖,我想要特定圖像的文本,我想爲特定圖像添加文本。我怎麼給我的文字到圖像的旋轉木馬如何在旋轉木馬下方添加文本視圖
public class Team extends Fragment {
private HorizontalCarouselStyle mStyle;
private HorizontalCarouselLayout mCarousel;
private CarouseAdapter mAdapter;
private ArrayList<Integer> mData = new ArrayList<Integer>(0);
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.team, container, false);
mData.add(R.drawable.s);
mData.add(R.drawable.t);
mData.add(R.drawable.tt);
mAdapter = new CarouseAdapter(getActivity());
mAdapter.setData(mData);
mCarousel = (HorizontalCarouselLayout) view.findViewById(R.id.carousel_layout);
mStyle = new HorizontalCarouselStyle(getActivity(), HorizontalCarouselStyle.NO_STYLE);
mCarousel.setStyle(mStyle);
mCarousel.setAdapter(mAdapter);
mCarousel.setOnCarouselViewChangedListener(new CarouselInterface() {
@Override
public void onItemChangedListener(View v, int position) {
Toast.makeText(getContext(), "Position " + String.valueOf(position), Toast.LENGTH_SHORT).show();
}
});
return view;
}
}
carouseradapter:
public class CarouseAdapter extends BaseAdapter {
private ArrayList<Integer> mData = new ArrayList<Integer>(0);
private Context mContext;
public CarouseAdapter(Context context) {
mContext = context;
}
public void setData(ArrayList<Integer> data) {
mData = data;
}
@Override
public int getCount() {
return mData.size();
}
@Override
public Object getItem(int pos) {
return mData.get(pos);
}
@Override
public long getItemId(int pos) {
return pos;
}
@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
ImageView mImage= new ImageView(mContext);
mImage.setImageResource(mData.get(arg0));
mImage.setPadding(5, 5, 5, 5);
mImage.setBackgroundResource(R.drawable.slider_bg);
return mImage;
}
}
佈局:
<LinearLayout 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:orientation="vertical"
android:background="#FFFFFF" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="@string/tb"
android:textColor="#EA90AF"
android:layout_gravity="center"
android:textSize="20sp"
android:layout_marginTop="10dp"
android:id="@+id/textView23" />
<com.touchmenotapps.carousel.simple.HorizontalCarouselLayout
android:id="@+id/carousel_layout"
android:layout_marginTop="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
爲什麼不在xml文件中添加「TextView」,然後根據傳送帶圖像更新'onItemChangedListener'中的值? – Rami
我已經嘗試過,但我不知道如何在傳送帶移動時更改文本。我們如何才能在onclicklistener – sun
中給出該圖像的特定位置的文本在'onItemChangedListener'中,您具有圖像的位置,所以您只需要獲取此圖像的文本。您可以使用exp來存儲文本的'ArrayList',然後您只需調用'mTextsList.get(position);' – Rami