设置背景属性viewpager android

时间:2017-07-10 11:57:33

标签: android android-viewpager

我有ViewPager每页显示两个项目,即我将getPageWidth设置为0.5fI需要在每个ViewPager项目上设置灰色圆角背景加载的图像从数据库中获取并位于背景上。我已经尝试将背景属性放在ViewPager本身和ImageView上,但他们所做的就是在整个ViewPager上设置一个静态的完整背景。请帮助实现这一目标。

以下是我的代码:

public class LandingPageAdapter extends PagerAdapter {
    private Context mContext;
    private ArrayList<String> drinkName, drinkCost, drinkImage, staffPick, bestSeller, trending;

    public LandingPageAdapter(Context context, ArrayList<String> drinkName,ArrayList<String> drinkCost,
                              ArrayList<String> drinkImage, ArrayList<String> staffPick,
                              ArrayList<String> bestSeller, ArrayList<String> trending) {
        mContext = context;
        this.drinkName = drinkName;
        this.drinkCost = drinkCost;
        this.drinkImage = drinkImage;
        this.staffPick = staffPick;
        this.bestSeller = bestSeller;
        this.trending = trending;

    }

    @Override
    public Object instantiateItem(ViewGroup container, final int position) {

        LayoutInflater inflater = LayoutInflater.from(mContext);
        View itemView = inflater.inflate(R.layout.page, container, false);
        final ImageView image1 = (ImageView)itemView.findViewById(R.id.image);
        TextView textName = (TextView) itemView.findViewById(R.id.name);
        TextView textCost = (TextView) itemView.findViewById(R.id.cost);
       // image1.setImageResource(images[position]);

        Log.d("imagess","http://159.203.104.154/shikatei/products/landingpage/"+drinkImage.get(position));
        Log.d("trends",staffPick.get(position)+"|"+bestSeller.get(position)+"|"+trending.get(position));
        Picasso.with(mContext)
                .load("http://159.203.104.154/shikatei/products/landingpage/"+drinkImage.get(position))
                .placeholder(R.drawable.bg111)
                .resize(1800,3000)
                .networkPolicy(NetworkPolicy.OFFLINE)
                .into(image1, new Callback() {
                    @Override
                    public void onSuccess() {
                        Log.d("succ","succ");
                    }

                    @Override
                    public void onError() {
                        Log.d("err","err");
                        //Try again online if cache failed
                        Picasso.with(mContext)
                                .load("http://159.203.104.154/shikatei/products/landingpage/"+drinkImage.get(position))
                                .error(R.drawable.bg111)
                                .resize(1800,3000)
                                .into(image1, new Callback() {
                                    @Override
                                    public void onSuccess() {

                                    }

                                    @Override
                                    public void onError() {
                                        Log.v("Picasso","Could not fetch image");
                                    }
                                });
                    }
                });

        textName.setText(drinkName.get(position));
        textCost.setText("Ksh "+drinkCost.get(position));

        ((ViewPager) container).addView(itemView);
        return itemView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position,
                            Object object) {
        container.removeView((View)object);
    }

    @Override
    public int getCount() {
        return(5);
    }

    @Override
    public float getPageWidth(int position) {
        return(0.5f);
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return(view == object);
    }
}


public class ImageTask extends AsyncTask<String,String, List<SpaceModel> > {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            // dialog.show();
        }

        @Override
        protected List<SpaceModel> doInBackground(String... params) {
            HttpURLConnection connection = null;
            BufferedReader reader = null;

            try {
                Log.d("activity: ", "SignInActivity");
                URL url = new URL(params[0]);
                connection = (HttpURLConnection) url.openConnection();
                connection.connect();
                InputStream stream = connection.getInputStream();
                reader = new BufferedReader(new InputStreamReader(stream));
                StringBuffer buffer = new StringBuffer();
                String line ="";
                while ((line = reader.readLine()) != null){
                    buffer.append(line);
                }

                String finalJson = buffer.toString();

                JSONObject parentObject = new JSONObject(finalJson);
                JSONArray parentArray = parentObject.getJSONArray("spaces");

                List<SpaceModel> spaceModelList = new ArrayList<>();

                Gson gson = new Gson();
                for(int i=0; i<parentArray.length(); i++) {
                    JSONObject finalObject = parentArray.getJSONObject(i);
                    /**
                     * below single line of code from Gson saves you from writing the json parsing yourself which is commented below
                     */
                    SpaceModel spaceModel = gson.fromJson(finalObject.toString(), SpaceModel.class);

                    spaceModelList.add(spaceModel);
                }
                return spaceModelList;

            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (JSONException e) {
                e.printStackTrace();
            } finally {
                if(connection != null) {
                    connection.disconnect();
                }
                try {
                    if(reader != null) {
                        reader.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return  null;
        }

        @Override
        protected void onPostExecute(final List<SpaceModel> result) {
            super.onPostExecute(result);
            // dialog.dismiss();
            int arrayCount = 0;
            if(result != null) {
                int count = 0;
                Iterator<Map.Entry> iterator = loc.entrySet().iterator();

                while (iterator.hasNext()) {
                    Map.Entry entry = (Map.Entry) iterator.next();
                    String value = (String) entry.getValue();
                    System.out.println("Key : " + entry.getKey() + " Value :" + entry.getValue());
                    String[] parts = value.split(";");
                    topPicksName.add(parts[1]);
                    topPicksCost.add(parts[2]);
                    topPicksImage.add(parts[3]);
                    staffPick.add(parts[4]);
                    bestSeller.add(parts[5]);
                    trending.add(parts[6]);
                }

                topPicksPager=(ViewPager) rootView.findViewById(R.id.top_picks_pager);
                topPicksPager.setAdapter(new LandingPageAdapter(getActivity(), topPicksName,
                        topPicksCost, topPicksImage, staffPick, bestSeller, trending));

                if (isNetworkAvailable() == true){

                } else {
                    showInternetDialog();
                }

            } else {
                Toast.makeText(getActivity(), "Not able to fetch data from server, please check url.", Toast.LENGTH_SHORT).show();
            }
        }
    }

Page xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical">


    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <app.android.taji.com.drinkdelivery.layout.GalleryTextView
        android:id="@+id/name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="13dp"
        android:layout_marginTop="235dp"
        android:gravity="center"
        android:text="Tito's Handmade Vodka"/>

    <app.android.taji.com.drinkdelivery.layout.GalleryTextView
        android:id="@+id/cost"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="13dp"
        android:layout_marginTop="250dp"
        android:gravity="center"
        android:text="$18.99 - $26.99"/>

</FrameLayout>

ViewPager xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:vpi="http://schemas.android.com/tools"
    android:orientation="vertical">
<ScrollView
    android:id="@+id/scrollView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:fillViewport="true"
    android:orientation="vertical" >
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:custom="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <com.daimajia.slider.library.SliderLayout
            android:id="@+id/slider"
            android:layout_width="wrap_content"
            android:layout_height="230dp"/>

        <com.daimajia.slider.library.Indicators.PagerIndicator
            android:id="@+id/custom_indicator"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            custom:selected_color="#0095BF"
            custom:unselected_color="#55333333"
            custom:shape="oval"
            />
        <com.daimajia.slider.library.Indicators.PagerIndicator
            android:id="@+id/custom_indicator2"
            style="@style/AndroidImageSlider_Corner_Oval_Orange"
            android:layout_centerHorizontal="true"
            android:layout_alignParentBottom="true"
            />
    </LinearLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:id="@+id/top_picks_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingLeft="10dp"
            android:paddingTop="15dp"
            android:paddingBottom="5dp"
            android:text="Top Picks"/>

        <android.support.v4.view.ViewPager
            android:id="@+id/top_picks_pager"
            android:layout_width="match_parent"
            android:layout_below="@+id/top_picks_text"
            android:background="#000000"
            android:layout_height="270dp"/>

        <TextView
            android:id="@+id/top_collections_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingBottom="5dp"
            android:paddingTop="15dp"
            android:paddingLeft="10dp"
            android:layout_below="@+id/top_picks_pager"
            android:text="Collections For You"/>

        <android.support.v4.view.ViewPager
            android:id="@+id/collections_pager"
            android:layout_width="match_parent"
            android:layout_below="@id/top_collections_text"
            android:layout_height="200dp"/>

    </RelativeLayout>
</LinearLayout>

</ScrollView>
</RelativeLayout>

0 个答案:

没有答案