更改网络上适配器中存在的图像视图的图像

时间:2018-07-08 04:55:21

标签: android android-viewpager imageview android-broadcastreceiver

在对问题投反对票之前,请先阅读问题。我知道当网络连接发生更改时如何更改ui,如果我们要更改ui中的activity,这非常容易。我将VerticalView Pager Adapter用于ViewPager。我想根据网络连接来更改位于适配器类中的imageview's映像。我的适配器类如下所示

class VerticlePagerAdapter(private val mContext: Context, private val newsArrayList: ArrayList<News?>) : PagerAdapter() {

    private var mLayoutInflater: LayoutInflater =
            mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater

    override fun getCount(): Int {
        return newsArrayList.size
    }

    override fun isViewFromObject(view: View, `object`: Any): Boolean {
        return view === `object`
    }

    override fun instantiateItem(container: ViewGroup, position: Int): Any {
        val itemView = mLayoutInflater.inflate(R.layout.content_main, container, false)

        val title = itemView.findViewById<TextView>(R.id.tv_content_main_title)
        val imageView = itemView.findViewById<ImageView>(R.id.iv_content_main)
        val description = itemView.findViewById<TextView>(R.id.tv_content_main_description)
        val progressBar = itemView.findViewById<ProgressBar>(R.id.progressBar)


        title.text = newsArrayList[position]?.title ?: "Sorry No Title Found"


            GlideApp.with(mContext).load(newsArrayList[position]?.imageUrl)
                    .error(R.mipmap.nointernet).diskCacheStrategy(DiskCacheStrategy.ALL).listener(object:RequestListener<Drawable>{
                        override fun onLoadFailed(e: GlideException?, model: Any?, target: Target<Drawable>?, isFirstResource: Boolean): Boolean {
                            progressBar.visibility = View.GONE
                            return false
                        }

                        override fun onResourceReady(resource: Drawable?, model: Any?, target: Target<Drawable>?, dataSource: DataSource?, isFirstResource: Boolean): Boolean {
                            progressBar.visibility = View.GONE
                            return false
                        }

                    }).into(imageView)


                override fun onResourceReady(resource: Drawable?, model: Any?, target: Target<Drawable>?, dataSource: DataSource?, isFirstResource: Boolean): Boolean {
                    progressBar.visibility = View.GONE
                    return false
                }

            }).into(imageView)

        }

        description.text = newsArrayList[position]?.description ?: "Sorry No Description Found"

        container.addView(itemView)

        return itemView
    }

    override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
        container.removeView(`object` as ConstraintLayout)
    }

}

以下是我的代码,用于更改网络更改的ui,但这是为了活动。

public class MainActivity extends AppCompatActivity {

    private MyReceiver myReceiver;
    private TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = findViewById(R.id.tv);
        myReceiver = new MyReceiver();
    }

    @Override
    protected void onResume() {
        super.onResume();
        IntentFilter intentFilter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
        registerReceiver(myReceiver,intentFilter);

        IntentFilter customFilter = new IntentFilter(MyReceiver.NOTIFY_NETWORK_CHANGE);
        LocalBroadcastManager.getInstance(this).registerReceiver(mLocalReceiver,customFilter);
    }

    @Override
    protected void onPause() {
        super.onPause();
        unregisterReceiver(myReceiver);

    }


    private BroadcastReceiver mLocalReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
         boolean isConnected = intent.getBooleanExtra(MyReceiver.EXTRA_IS_CONNECTED,false);
         if(isConnected){
             //chnage the ui
         }else{
             //change the ui
         }
        }
    };
}

如果我在适配器中使用上面的代码,如何获取image view的{​​{1}}中onReceive的位置参数。

或者可以在活动本身中使用代码并在网络更改时更改适配器的ui。 我知道我可以在网络变化时重新加载活动,但这不是很好的用户体验,也不是正确的方法,因为用户可以清楚地看到活动已重新加载。

1 个答案:

答案 0 :(得分:0)

最简单的解决方案可能是在网络连接发生更改时调用adapter.notifyDataSetChanged()。因此,您的代码保持不变,只需在adapter.notifyDataSetChanged()方法中添加mLocalReceiver#onReceive()

缺点是,从本质上讲,您将要求适配器重绘每一项,而这可能并不是您所需要的。但是,根据您屏幕的复杂程度,此解决方案可能会足够好。