Android实战项目之十三-----图片的浏览器的实现

效果图

 一、xml布局

<androidx.coordinatorlayout.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:theme="@style/SplashTheme"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/Primary">
    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="@color/text_3"
        android:background="@color/Primary">
        <androidx.appcompat.widget.Toolbar
            android:id="@+id/bar_culture"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimaryDark"
            app:popupTheme="@style/AppTheme.PopupOverlay">
            <TextView
               android:id="@+id/culture_tool_title"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:layout_centerInParent="true"
               android:layout_gravity="center"
               android:text="宴席详情"
               android:textColor="#111"
               android:textStyle="bold"
               android:maxEms="8"
               android:singleLine="true"
               android:ellipsize="end"
               android:textSize="20sp" />
         </androidx.appcompat.widget.Toolbar>
    </com.google.android.material.appbar.AppBarLayout>
    <RelativeLayout 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"
        tools:context=".activity.TableActivity">

        <androidx.viewpager.widget.ViewPager
            android:id="@+id/viewpager"
            android:background="#000"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
        </androidx.viewpager.widget.ViewPager>

        <LinearLayout
            android:id="@+id/points"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:gravity="center"
            android:layout_alignBottom="@+id/viewpager"
            android:layout_marginBottom="30dp"
            android:padding="5dp"></LinearLayout>
    </RelativeLayout>


</androidx.coordinatorlayout.widget.CoordinatorLayout>

二、TableActivity核心代码实现

 

public class TableActivity extends BaseActivity {
    ..................略.............

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_table);

        //获取传参
        Intent intent=getIntent();
        Bundle bundle=intent.getExtras();
        if(bundle!=null){
            tableTitle = bundle.getString("tableTitle");
            tableID=bundle.getString("tableId");
        }
        Toolbar toolbar=findViewById(R.id.bar_culture);
        setSupportActionBar(toolbar);
        ActionBar actionBar =  getSupportActionBar();
        if(actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setDisplayShowTitleEnabled(false);
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
        /**
        Toolbar toolbar=findViewById(R.id.toolbar);
        TextView titleBar=findViewById(R.id.toolbar_title);
        titleBar.setText(tableTitle);
        //toolbar.setTitle(tableTitle);
        setSupportActionBar(toolbar);
        ActionBar actionBar =  getSupportActionBar();
        if(actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setDisplayShowTitleEnabled(false);
            actionBar.setDisplayHomeAsUpEnabled(true);
        }**/
        mDatas=new ArrayList<>();
        initDatas();
        //initView();
        //initData();
        //photoView = findViewById(R.id.photoView);
        //loadImages();
        /**
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        //mImg = findViewById(R.id.id_content);
        //mRecyclerView =  findViewById(R.id.id_recyclerview_horizontal);
        mRecyclerView =  findViewById(R.id.recyclerView);

        mRecyclerView.setLayoutManager(linearLayoutManager);
        //mAdapter = new RecycleViewAdapter(this, mDatas);
        mAdapter=new ImageAdapter(mDatas,this);
        mRecyclerView.setAdapter(mAdapter);
        //mImg.setImageResource(mDatas.get(0));
        /**
        mRecyclerView.setOnItemScrollChangeListener(new PreviewRecycleView.OnItemScrollChangeListener()
        {
            @Override
            public void onChange(View view, int position)
            {
                Glide.with(mActivity).load(mDatas.get(position)).thumbnail(0.1f).into(mImg);
                Log.d("tatatatatd111", String.valueOf(mDatas.get(position)));
            };
        });


        mAdapter.setOnItemClickLitener((view, position) -> {
            //Toast.makeText(getApplicationContext(), position + "", Toast.LENGTH_SHORT).show();
            //mImg.setImageResource(mDatas.get(position));
            Glide.with(mActivity).load(mDatas.get(position)).thumbnail(0.1f).into(mImg);
            //mAdapter.notifyDataSetChanged();
            Log.d("tatatatat", String.valueOf(position));
        });**/

    }

    @Override
    public boolean onSupportNavigateUp() {
        onBackPressed();
        return true;
    }



    private void initDatas() {

        
        HttpRequest.tableitem(TableActivity.this, tableID, new CallBackLis<TableBean>() {
            @Override
            public void onSuccess(String method, TableBean content) {
                mDatas.clear();
                List<String> result = Arrays.asList(content.getThumbs().split(","));
                Log.d("tatatatat", String.valueOf(result));
                for(String   data    :    result){
                   ........
                }
                imglist= mDatas.toArray(new String[0]);
                Log.d("tatatatat111", String.valueOf(mDatas));
                //myViewPagerAdapter = new ImagePagerAdapter(mActivity,imglist);
                //viewPager.setAdapter(myViewPagerAdapter);
                initView();
                initData();

            }
            @Override
            public void onFailure(String method, String error) {
                Toast.makeText(TableActivity.this, error, Toast.LENGTH_SHORT).show();
            }
        });


        );


    }


    private void initView() {
        viewPager = findViewById(R.id.viewpager);
        myViewPagerAdapter = new ImagePagerAdapter(this,imglist);
        viewPager.setAdapter(myViewPagerAdapter);
        points = findViewById(R.id.points);
        for(int i = 0;i<imglist.length;i++) {
            //白点
            //根据viewPager的数量,添加白点指示器
            ImageView view = new ImageView(this);
            view.setBackgroundResource(R.drawable.point_back);
            //给点设置宽高
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(20, 20);
            //给控件设置边距
            params.leftMargin = 10;
            //给view设置参数
            view.setLayoutParams(params);
            //将图片添加到线性布局中
            points.addView(view);
        }

        points.getChildAt(0).setBackgroundResource(R.drawable.point_white);
        viewPager.setCurrentItem(0);
    }
    private void initData() {
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float v, int i1) {
                position = position % imglist.length;
                //把前一个白变为黑
                points.getChildAt(prePosition).setBackgroundResource(R.drawable.point_back);
                //把当前白点变为黑点
                points.getChildAt(position).setBackgroundResource(R.drawable.point_white);
                //记录下当前位置(当前位置变白后,赋值给前一个点)
                prePosition = position;
            }

            @Override
            public void onPageSelected(int i) {

            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });
    }


}

本功能写的粗糙,等有空再细化,功能基本实现。欢迎留言交流技术。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值