面試題:ListView每個Item倒計時實現

那個。。。這個是之前面試的時候被問到的問題,那時給一個半小時要求實現這個功能,結果乾坐一個小時沒實現- -。 灰頭土臉的走了。。。現在重新把這個實現了,然後貼出來吧。   只有代碼,具體的思路不難,看一下就能看出來了。

先從最簡單的佈局來吧

MainAcrivity佈局:

<LinearLayout
    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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.administrator.hongyangzzw.MainActivity">

    <ListView
        android:id="@+id/lv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>


</LinearLayout>

ListView Item 佈局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="horizontal">


    <TextView
        android:id="@+id/tv"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:gravity="left"
        android:textSize="35dp"/>

    <com.example.administrator.hongyangzzw.TimeView
        android:gravity="right"
        android:textSize="35dp"
        android:id="@+id/timetv"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"/>


</LinearLayout>

那個TimeView 是自定義控件  繼承TextView 

TimeView

public class TimeView extends TextView implements  Runnable{

    private int position;

    public void setPosition(int position) {
        this.position = position;
    }

    @Override
    public void run() {
        String time= MyApplication.get(position, "time");
        setText(time);
        if(!"售罄".equals(time)){
            postDelayed(this, 1000);
        }
    }

    public TimeView(Context context) {
        super(context , null);
    }

    public TimeView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs , 0);
    }

    public TimeView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        postDelayed(this , 1000);
    }


}

MyApplication

public class MyApplication extends Application {

    private static List<Map<String, String>> list;

    @Override
    public void onCreate() {
        super.onCreate();
    }

    public static void set(List<Map<String, String>> l) {
        list = l;
    }

    public static String get(int position, String key) {
        return list.get(position).get(key);
    }

    public static void removeAll(){
        if (list!=null)
            list.clear();
        list = null;
    }

}
manifests 別忘了 註冊application

MyAdapter

public class MyAdapyer extends BaseAdapter{

    private List<Map<String , String>> data;
    private Context mContext;

    public MyAdapyer(Context context) {
        mContext = context;
    }

    public void setData(List<Map<String, String>> data) {
        this.data = data;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null){
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item , parent , false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.tv.setText(data.get(position).get("name") + "");
        holder.timetv.setText(data.get(position).get("time") + "");
        holder.timetv.setPosition(position);
        return convertView;
    }


    class ViewHolder{
        private TextView tv;
        private TimeView timetv;
        public ViewHolder(View view) {
            tv =view.findViewById(R.id.tv);
            timetv = view.findViewById(R.id.timetv);
        }
    }




}

最後是MainActivity:

public class MainActivity extends AppCompatActivity {

    public List<Map<String, String>> list;
    private ListView lv;
    private MyAdapyer mMyAdapyer;
    private BroadcastReceiver br;
    private IntentFilter intentFilter;


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

        Map<String, String> map = null;
        list = new ArrayList<Map<String, String>>();
        for (int i = 1; i < 50; i++) {
            map = new HashMap<String, String>();
            map.put("name", "秒殺產品" + i);
            map.put("time", String.valueOf(10 * i));
            list.add(map);
        }
        MyApplication.set(list);
        // 數據拿到開始計時
        lv = (ListView) findViewById(R.id.lv);
        mMyAdapyer = new MyAdapyer(this);

        mMyAdapyer.setData(list);
        lv.setAdapter(mMyAdapyer);

        start();

        br = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                mMyAdapyer.notifyDataSetChanged();
            }
        };

        intentFilter = new IntentFilter();
        intentFilter.addAction("asdasdasd");
        registerReceiver(br, intentFilter);

    }


    int result = 0;
    private Thread thread;

    public void start() {
        thread = new Thread() {
            public void run() {
                while (true) {
                    try {
                        if (list == null || result == list.size()) {
                            break;
                        }
                        sleep(1000);
                        //發廣播通知
                        Intent intent = new Intent("asdasdasd");
                        sendBroadcast(intent);

                        for (Map<String, String> map : list) {
                            if (!"售罄".equals(map.get("time"))) {
                                if ("1".equals(map.get("time"))) {
                                    map.put("time", "售罄");
                                    result++;
                                } else {
                                    map.put("time", "" + (Integer.parseInt(map.get("time")) - 1));
                                }
                            }
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }

            ;
        };
        thread.start();
    }

    @Override
    protected void onDestroy() {
        MyApplication.removeAll();
        list = null;
        unregisterReceiver(br);
        super.onDestroy();
    }


}

------------------------------------------------------------------------------------------------------------------

OK    搞定   最麻煩的是Item劃出頁面還要計時這一點。。。    如果有人遇到這種類似的面試上機題的話   就算幫你忙啦

如果幫上了  記得 來  評論點贊- -。

發佈了104 篇原創文章 · 獲贊 138 · 訪問量 15萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章