我正在开发一个应用程序,其中有许多产品存储在sqlite数据库中。
我制作了一个水平滚动的Recycler视图和Cardview。但它自己的卷轴太平滑,孩子不会停在中心。
只能显示一个孩子(例如在标签浏览器中)。
我是否应该使用viewpager来执行此操作,但必须创建这么多片段。
答案 0 :(得分:2)
import android.app.Activity;
import android.os.Build;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import com.retail.R;
import com.retail.beacons.beaconModel.OfferBeaconData;
import com.retail.manager.UserProfileManager;
import com.retail.ui.view.RobotoLightTextVew;
import com.squareup.picasso.Picasso;
import java.util.List;
/**
* Created by mahendra.chhimwal on 1/14/2016.
*/
public class BeaconOffersPagerAdapter extends PagerAdapter {
private Activity mContext;
private List<OfferBeaconData.OfferItem> mOfferItems;
public BeaconOffersPagerAdapter(Activity context, List<OfferBeaconData.OfferItem> offerItems) {
this.mContext = context;
this.mOfferItems = offerItems;
}
@Override
public int getCount() {
return mOfferItems == null ? 0 : mOfferItems.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public Object instantiateItem(final ViewGroup container, final int position) {
View view;
LayoutInflater inflater = LayoutInflater.from(mContext);
view = inflater.inflate(R.layout.item_beac_off_pager_item, container, false);
ImageView offerItemImage = (ImageView) view.findViewById(R.id.iv_offer_image);
RobotoLightTextVew tvWelcomeMsg = (RobotoLightTextVew) view.findViewById(R.id.tv_welcome_msg);
RobotoLightTextVew upperTitle = (RobotoLightTextVew) view.findViewById(R.id.tv_upper_title);
RobotoLightTextVew bottomTitle = (RobotoLightTextVew) view.findViewById(R.id.tv_bottom_title);
RobotoLightTextVew discountText = (RobotoLightTextVew) view.findViewById(R.id.tv_offer_discount);
//Updating views
Picasso.with(mContext).load(mOfferItems.get(position).getOfferProduct().getImageUri()).into(offerItemImage);
upperTitle.setText(mOfferItems.get(position).getOfferTitle());
String userName = UserProfileManager.getInstance().getName();
if (userName != null && userName.length() > 0) {
int toIndex = userName.indexOf(" ");
if (toIndex != -1) {
userName = userName.substring(0, toIndex);
}
tvWelcomeMsg.setVisibility(View.VISIBLE);
tvWelcomeMsg.setText(mContext.getString(R.string.welcome_greeting, userName));
}
bottomTitle.setText(mOfferItems.get(position).getOfferSecondTitle());
discountText.setText("" + mOfferItems.get(position).getOfferProduct().getMaxDiscount() + "% off");
return view;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
}
您可以使用Simple PagerAdapter进行此操作。像RecyclerViewAdapter一样夸大你的观点。以上是我在我的代码中使用的示例示例。 也不需要使用Fragment。
答案 1 :(得分:0)
仅用于兼容Android Lollipop。 Android Lollipop两个新的小部件让你的生活更轻松,RecyclerView和CardView.you必须创建简单的标签浏览器