Android Open Source - SwitchTabs Placeholder Fragment






From Project

Back to project page SwitchTabs.

License

The source code is released under:

Apache License

If you think the Android project SwitchTabs listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.normal.testdemo.fragments;
/*from   www. j a  v  a  2  s  .  c om*/
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.TextView;
import android.widget.Toast;
import com.normal.testdemo.R;
import com.normal.testdemo.activitys.MainActivity;
import com.normal.testdemo.activitys.SplashActivity;
import com.normal.testdemo.adapters.HotelListAdapter;
import com.normal.testdemo.beans.Hotel;
import com.normal.testdemo.utils.Utils;
import com.normal.testdemo.view.XListView;

import java.util.List;

/**
 * Created by ex_chenjinghao on 2014/4/8.
 * A placeholder fragment containing a simple view.
 */
public class PlaceholderFragment extends Fragment implements XListView.IXListViewListener {
  /**
   * The fragment argument representing the section number for this
   * fragment.
   */
  private static final String ARG_SECTION_NUMBER = "section_number";
  private static int refreshCnt = 0;
  //  private ListView lv_hotels; // ?????ListView
  private XListView lv_hotels; // ?????ListView
  private List<Hotel> hotels; // ?????ListView?????
  private SwipeRefreshLayout refresh;//???????
  private int start = 0;
  private Handler mHandler;
  private HotelListAdapter mAdapter;

  /**
   * Returns a new instance of this fragment for the given section number.
   */
  public static PlaceholderFragment newInstance(int sectionNumber) {
    PlaceholderFragment fragment = new PlaceholderFragment();
    Bundle args = new Bundle();
    args.putInt(ARG_SECTION_NUMBER, sectionNumber);
    fragment.setArguments(args);
    return fragment;
  }

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // ??????????Utils??????????
    Utils.cacheDir = getActivity().getCacheDir();
    // ?intent?????????
    Intent intent = getActivity().getIntent();
    hotels = intent.getParcelableArrayListExtra("hotels");
  }

  // ???????????????????????????id??View???view????????????
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    mHandler = new Handler();
    // ??????????Utils??????????
    Utils.cacheDir = MainActivity.context.getCacheDir();
    // ?intent?????????
    Intent intent = getActivity().getIntent();
    hotels = intent.getParcelableArrayListExtra("hotels");
    Log.i("hotels", hotels + "");
    // ?????????????
    View rootView = inflater.inflate(R.layout.fragment_main, container,false);
    // textView???????????????????????
    assert rootView != null;
    TextView textView = (TextView) rootView.findViewById(R.id.section_label);
    textView.setText(Integer.toString(getArguments().getInt(ARG_SECTION_NUMBER)));
//    refresh = (SwipeRefreshLayout) rootView.findViewById(R.id.refresh);//??????
//    refresh.setOnRefreshListener(this);//???????????
//    refresh.setColorScheme(R.color.red,R.color.blue,R.color.green,R.color.black);//?????????
    lv_hotels = (XListView) rootView.findViewById(R.id.lv_hotels);
    lv_hotels.setAdapter(new HotelListAdapter(getActivity(), hotels));
    lv_hotels.setPullLoadEnable(true);
    lv_hotels.setXListViewListener(this);
    Log.i("setAdapter","????????");
    // ????????android?????????????????java????????????
    lv_hotels.setOnItemClickListener(new AdapterView.OnItemClickListener() {
      @Override
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (position <= hotels.size()) {
          Hotel h = hotels.get(position);
          // ???????????????inflate?????????????????????
          View dialogView = getActivity().getLayoutInflater().inflate(R.layout.dialog_view, null);
          TextView tv_hotelName = (TextView) dialogView.findViewById(R.id.tv_hotel_name);
          TextView tv_streetAddress = (TextView) dialogView.findViewById(R.id.tv_street_address);
          TextView tv_nightlyRate = (TextView) dialogView.findViewById(R.id.tv_nightly_rate);
          AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
          // ????????????????????????????????????????null
          builder.setTitle("Hotel info :").setView(dialogView).setPositiveButton("??", null);
          tv_hotelName.setText(h.getName());
          tv_hotelName.setTextColor(getResources().getColor(R.color.black));
          tv_streetAddress.setText(h.getStreet_address());
          tv_streetAddress.setTextColor(getResources().getColor(R.color.black));
          tv_nightlyRate.setText(h.getNightly_rate());
          tv_nightlyRate.setTextColor(getResources().getColor(R.color.black));
          builder.show();
        }
      }
    });
    return rootView;
  }

  @Override
  public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
  }

/*  @Override
  public void onRefresh() {
    new Handler().postDelayed(new Runnable() {
      public void run() {
        refresh.setRefreshing(false);
        Toast.makeText(getActivity(), "????",Toast.LENGTH_SHORT).show();
//        adapter.notifyDataSetChanged();
      }
    }, 1000);
  }*/

  private void geneItems() {
    for (int i = 0; i != 5; ++i) {
      hotels.add(new Hotel(null, "new " + (++start), "new address", "new nightly rate"));
    }
  }

  private void onLoad() {
    lv_hotels.stopRefresh();
    lv_hotels.stopLoadMore();
    lv_hotels.setRefreshTime("??");
  }

  @Override
  public void onRefresh() {
    mHandler.postDelayed(new Runnable() {
      @Override
      public void run() {
//        start = ++refreshCnt;
        new SplashActivity.GetJson().execute(SplashActivity.JSON_URL);
        hotels.clear();
        // ?intent?????????
        hotels = SplashActivity.hotels.getHotels();
        mAdapter = new HotelListAdapter(getActivity(), hotels);
//        mAdapter.notifyDataSetChanged();
        lv_hotels.setAdapter(mAdapter);
        onLoad();
      }
    }, 2000);
    Toast.makeText(getActivity(), "???????", Toast.LENGTH_SHORT).show();
  }

  @Override
  public void onLoadMore() {
    mHandler.postDelayed(new Runnable() {
      @Override
      public void run() {
        geneItems();
        mAdapter.notifyDataSetChanged();
        onLoad();
      }
    }, 2000);
    Toast.makeText(getActivity(), "????????", Toast.LENGTH_SHORT).show();
  }
}




Java Source Code List

com.normal.testdemo.activitys.MainActivity.java
com.normal.testdemo.activitys.SplashActivity.java
com.normal.testdemo.adapters.HotelListAdapter.java
com.normal.testdemo.adapters.SectionsPagerAdapter.java
com.normal.testdemo.beans.Hotel.java
com.normal.testdemo.beans.Hotels.java
com.normal.testdemo.fragments.PlaceholderFragment.java
com.normal.testdemo.utils.AsynImageLoader.java
com.normal.testdemo.utils.Utils.java
com.normal.testdemo.view.XListViewFooter.java
com.normal.testdemo.view.XListViewHeader.java
com.normal.testdemo.view.XListView.java