آموزش های این وب سایت به صورت رایگان در دسترس است. اطلاعات بیشتر
مشکل عدم دسترسی خریداران پیشین به برخی آموزش ها برطرف شد
بروز خطا
   [message]
اشتراک در سوال
رای ها
[dataList]

یک Adapter برای دوتا لیست ویو با داده های متفاوت

Noxius  8 سال پیش  8 سال پیش
+2 0

  • چطوری میشه از یک کلاس Adapter برای دوتا لیست ویو استفاده کرد ؟ 

    توی ActivityMain از Viewpager استفاده کردم -  دو تا لیست ویو داره که با Swipe تغییر میکنه

    یک کلاس ساختم به نام AdapterProduct میخوام برای هر دو لیست ویو ازش استفاده کنم با داده های متفاوت

  •  ازش یه ابجکت ساختم  خواستم  متد fill رو براش جداگانه ست کنم یا به عنوان انترفست -> کلا جواب نداد

    راه ساده اینه که برای هر کدام جداگانه بسازم  و مقادیر را ست کنم.

  • از مدل لیست ویو استاد استفاده میکنم.

 برای این سوال 1 پاسخ وجود دارد.
پاسخ به سوال 
Criss  8 سال پیش
+2 0

زمان ساختن هر لیست ویو با استفاده از اداپتر اون به اون ایدی بدید < میتونید اطلاعات مورد نیاز رو هم بهش ارسال کنید < اداپتر زیر برای چهارتا لیست ویو کار میکنه و میتونه هر چنتا که دلتون میخاد بهش اضاف کنید

  adapter = new AdapterMosalla(structList, type, serverIp);
listView.setAdapter(adapter);
adapter.notifyDataSetChanged();

بعدش با توجه به type که در اینجا یک ایدی بر اساس String هست لیست ویو UI رو تغییر میده و کد های مربوطه رو اجرا میکنه

public class AdapterMosalla extends ArrayAdapter<StructValues> {

private static String type;
private static String serverIp;


public AdapterMosalla(ArrayList<StructValues> array, String pType, String pServerIp) {
super(G.context, R.layout.adapter_mosalla, array);
type = pType;
serverIp = pServerIp;
}


private static class ViewHolder {

public LinearLayout lytRoot;

public LinearLayout lytVideo;
public LinearLayout lytImage;
public LinearLayout lytSound;
public LinearLayout lytText;

public TextView txtVideoName;
public ImageView imgVideoTumb;

public TextView txtImageName;
public ImageView imgImage;

public TextView txtSoundName;
public ImageView imgSound;

public TextView txtTextName;


public ViewHolder(View view) {

lytRoot = (LinearLayout) view.findViewById(R.id.lytRootMosallaAdapter);

lytVideo = (LinearLayout) view.findViewById(R.id.lytVideoMosallaAdapter);
lytImage = (LinearLayout) view.findViewById(R.id.lytImageMosallaAdapter);
lytSound = (LinearLayout) view.findViewById(R.id.lytSoundMosallaAdapter);
lytText = (LinearLayout) view.findViewById(R.id.lytTextMosallaAdapter);

txtVideoName = (TextView) view.findViewById(R.id.txtVideoNameMosallaAdapter);
imgVideoTumb = (ImageView) view.findViewById(R.id.imgVideoMosallaAdapter);

txtImageName = (TextView) view.findViewById(R.id.txtImageNameMosallaAdapter);
imgImage = (ImageView) view.findViewById(R.id.imgImageMosallaAdapter);

txtSoundName = (TextView) view.findViewById(R.id.txtSoundNameMosallaAdapter);
imgSound = (ImageView) view.findViewById(R.id.imgSoundMosallaAdapter);

txtTextName = (TextView) view.findViewById(R.id.txtTextNameMosallaAdapter);

lytVideo.setVisibility(View.GONE);
lytImage.setVisibility(View.GONE);
lytSound.setVisibility(View.GONE);
lytText.setVisibility(View.GONE);

setViewOption();
}


private void setViewOption() {

if (type.equals("video")) {
lytVideo.setVisibility(View.VISIBLE);
}
if (type.equals("image")) {
lytImage.setVisibility(View.VISIBLE);
}
if (type.equals("sound")) {
lytSound.setVisibility(View.VISIBLE);
}
if (type.equals("text")) {
lytText.setVisibility(View.VISIBLE);
}
}

String itemUri = "";
String itemName = "";


public void fill(final ArrayAdapter<StructValues> adapter, final StructValues item, final int position) {
setColors();

setUrlAndName(item.mosalla_itemName);

if (type.equals("video")) {
txtVideoName.setText(itemName);
lytVideo.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
startVideoIntent("http://" + serverIp + "/mosalla/video/" + itemUri, itemName);
}
});
}

if (type.equals("image")) {
txtImageName.setText(itemName);
Glide.with(G.context).load("http://" + serverIp + "/mosalla/image/" + itemUri).into(imgImage);
imgImage.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
startDownload("http://" + v + "/mosalla/image/" + itemUri, itemName);
}
});
}

if (type.equals("sound")) {
txtSoundName.setText(itemName);
lytSound.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// startVideoIntent("http://192.168.1.3/mosalla/sound/" + itemUri, itemName);
startVideoIntent("http://" + serverIp + "/mosalla/sound/" + itemUri, itemName);
}
});
}

if (type.equals("text")) {
txtTextName.setText(itemName);
lytText.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
startDownload("http://" + serverIp + "/mosalla/text/" + itemUri, itemName);
}
});
}
}


private void setUrlAndName(String item) {
itemUri = item;
try {
itemName = (URLDecoder.decode(itemUri, "UTF-8"));
}
catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}


private void startDownload(String path, String title) {
G.toast("دانلود شروع شد", false, false);
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(path));
request.setDescription("در حال دانلود" + title);
request.setTitle(title);
// in order for this if to run, you must use the android 3.2 to compile your app
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
}
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, title);

// get download service and enqueue file
DownloadManager manager = (DownloadManager) G.context.getSystemService(Context.DOWNLOAD_SERVICE);
manager.enqueue(request);
}


private void startVideoIntent(String path, String title) {
Intent intent = new Intent(G.context, ActivityVideo.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("PATH", path);
intent.putExtra("TITLE", title);
G.context.startActivity(intent);
}


private void setColors() {
lytRoot.setBackgroundColor(Color.parseColor(G.lightColor));
lytVideo.setBackgroundColor(Color.parseColor(G.darkColor));
lytImage.setBackgroundColor(Color.parseColor(G.darkColor));
lytSound.setBackgroundColor(Color.parseColor(G.darkColor));
lytText.setBackgroundColor(Color.parseColor(G.darkColor));

txtVideoName.setTextColor(Color.parseColor(G.lightColor));
txtImageName.setTextColor(Color.parseColor(G.lightColor));
}
}

private int lastPosition = -1;


@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
StructValues item = getItem(position);
if (convertView == null) {
convertView = G.inflater.inflate(R.layout.adapter_mosalla, parent, false);
holder = new ViewHolder(convertView);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.fill(this, item, position);
/*
ObjectAnimator animation = (ObjectAnimator) AnimatorInflater.loadAnimator(getContext(), (position > lastPosition) ? R.anim.push_down_in : R.anim.push_up_in);
animation.setTarget(convertView);
animation.setDuration(500);
animation.start();
lastPosition = position;*/
return convertView;
}
}

 


پاسخگویی و مشاهده پاسخ های این سوال تنها برای اعضای ویژه سایت امکان پذیر است .
چنانچه تمایل دارید به همه بخش ها دسترسی داشته باشید میتوانید از این بخش لایسنس این آموزش را خریداری نمایید .