MyPagerAdapter .class
package news.com.mynews.adapter;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.Typeface;
import android.support.v4.view.PagerAdapter;
import android.support.v7.widget.ActionBarOverlayLayout.LayoutParams;
import android.text.Html;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.squareup.picasso.Picasso;
import java.util.ArrayList;
import news.com.mynews.R;
import news.com.mynews.bean.NewsBean;
/** * Created by Mafiree on 12/19/2016. *///http://android-er.blogspot.in/2014/04/example-of-viewpager-with-custom.htmlpublic class MyPagerAdapter extends PagerAdapter {
int NumberOfPages = 5;
int[] res = {
android.R.drawable.ic_dialog_alert,
android.R.drawable.ic_menu_camera,
android.R.drawable.ic_menu_compass,
android.R.drawable.ic_menu_directions,
android.R.drawable.ic_menu_gallery};
int[] backgroundcolor = {
0xFF101010,
0xFF202020,
0xFF303030,
0xFF404040,
0xFF505050};
Activity activity;
ArrayList<NewsBean> list;
public MyPagerAdapter(Activity activity,ArrayList<NewsBean> list) {
this.activity=activity;
this.list=list;
}
@Override
public int getCount() {
//return NumberOfPages; return list.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imageView=new ImageView(activity);
imageView.setScaleType(ScaleType.FIT_XY);
TextView textView = new TextView(activity);
textView.setTextColor(Color.BLACK);
// textView.setTextSize(18); textView.setTypeface(Typeface.DEFAULT_BOLD);
//textView.setText(String.valueOf(position)); // textView.setText(list.get(position).getTitle()); int n=200;
String upToNCharacters = list.get(position).getDescription().substring(0, Math.min(list.get(position).getDescription().length(), n));
textView.setText(Html.fromHtml("<h2>"+list.get(position).getTitle()+"</h2><br><p>"+upToNCharacters+" here</p>"));
TextView textContent=new TextView(activity);
// textContent.setText(Color.WHITE); textContent.setTextColor(Color.GRAY);
// textContent.setTextSize(16); // textContent.setText(Html.fromHtml("<h2>Title</h2><br><p>Description here</p>")); System.out.println("*******************************************");
String imageUrl=null;
imageUrl="http://bestcinema.in/wp-content/uploads/"+list.get(position).getImageURL();
try {
Picasso.with(activity).load(imageUrl).resize(250,350).into(imageView);
}catch (Exception e){
e.printStackTrace();
}
LayoutParams imageParams = new LayoutParams(
LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
imageView.setLayoutParams(imageParams);
LinearLayout layout = new LinearLayout(activity);
layout.setOrientation(LinearLayout.VERTICAL);
LayoutParams layoutParams = new LayoutParams(
LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
//layout.setBackgroundColor(Color.BLUE); layout.setLayoutParams(layoutParams);
layout.addView(imageView);
layout.addView(textView);
layout.addView(textContent);
final int page = position;
layout.setPadding(7,7,7,7);
LinearLayout layoutBotom = new LinearLayout(activity);
layoutBotom.setOrientation(LinearLayout.HORIZONTAL);
LayoutParams layoutParamsBottom = new LayoutParams(
LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
//layout.setBackgroundColor(Color.BLUE); layoutBotom.setLayoutParams(layoutParamsBottom);
ImageView imageViewShare=new ImageView(activity);
imageViewShare.setBackgroundResource(R.drawable.ic_menu_share);
ImageView imageViewSend=new ImageView(activity);
imageViewSend.setBackgroundResource(R.drawable.ic_menu_send);
layoutBotom.addView(imageViewShare);
layoutBotom.addView(imageViewSend);
layout.addView(layoutBotom);
container.addView(layout);
return layout;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((LinearLayout)object);
}
}
For Static PagerAdapter
class
CustomPagerAdapter
extends
PagerAdapter {
Context mContext;
LayoutInflater mLayoutInflater;
public
CustomPagerAdapter(Context context) {
mContext = context;
mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public
int
getCount() {
return
mResources.length;
}
@Override
public
boolean
isViewFromObject(View view, Object object) {
return
view == ((LinearLayout) object);
}
@Override
public
Object instantiateItem(ViewGroup container,
int
position) {
View itemView = mLayoutInflater.inflate(R.layout.pager_item, container,
false
);
ImageView imageView = (ImageView) itemView.findViewById(R.id.imageView);
imageView.setImageResource(mResources[position]);
container.addView(itemView);
return
itemView;
}
@Override
public
void
destroyItem(ViewGroup container,
int
position, Object object) {
container.removeView((LinearLayout) object);
}
}
No comments:
Post a Comment