Code Monkey home page Code Monkey logo

android-starter-kit's Introduction

引入权限

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>

编写自己的Application继承StarterKitApp

public class YourApp extends StarterKitApp {

  @Override public void onCreate() {
    super.onCreate();
    // TODO your code
  }
}

编写xml布局文件list_item_tweet.xml

<?xml version="1.0" encoding="utf-8"?>
<com.smartydroid.android.starter.kit.widget.ForegroundLinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:background="#FFFFFF"
    android:orientation="vertical"
    >
  <TextView
      android:id="@+id/text_tweet_content"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_marginLeft="@dimen/activity_horizontal_margin"
      android:layout_marginRight="@dimen/activity_horizontal_margin"
      android:layout_marginTop="8dp"
      android:maxLines="5"
      android:textColor="#636363"
      android:textSize="18sp"
      />

  <RelativeLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_marginLeft="@dimen/activity_horizontal_margin"
      android:layout_marginRight="@dimen/activity_horizontal_margin"
      android:layout_marginTop="4dp"
      android:paddingBottom="8dp"
      >

    <TextView
        android:id="@+id/text_tweet_source"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:singleLine="true"
        android:textColor="#919191"
        android:textSize="14sp"
        />

    <TextView
        android:id="@+id/text_tweet_published_time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_marginLeft="16dp"
        android:singleLine="true"
        android:text="12:48"
        android:textColor="#919191"
        android:textSize="14sp"
        />
  </RelativeLayout>
</com.smartydroid.android.starter.kit.widget.ForegroundLinearLayout>

自定义ViewHolder,继承EasyViewHolder

public class TweetViewHolder extends EasyViewHolder<Tweet> {

  @Bind(R.id.text_tweet_content) TextView mTweetContent;
  @Bind(R.id.text_tweet_published_time) TextView mTweetPublishAt;
  @Bind(R.id.text_tweet_source) TextView mTweetSource;

  public TweetViewHolder(Context context, ViewGroup parent) {
    super(context, parent, R.layout.list_item_tweet);
    ButterKnife.bind(this, itemView);
  }

  @Override public void bindTo(Tweet tweet) {
    mTweetContent.setText(tweet.content);
    mTweetSource.setText(tweet.source);
    mTweetPublishAt.setText(String.valueOf(tweet.publishedAt));
  }
}

Retrofit Service

public interface TweetService {

  @GET("api/v1/tweet/list") Call<Collection<Tweet>> getTweetList(
      @Query("page") int page,
      @Query("page_size") int pageSize);
}

Retrofit Client

public class StarterNetwork {

  private static final String sBaseUrl = "http://duanzi.net/";
  private Retrofit mRetrofit;

  // Make this class a thread safe singleton
  private static class SingletonHolder {
    private static final StarterNetwork INSTANCE = new StarterNetwork();
  }

  public static synchronized StarterNetwork get() {
    return SingletonHolder.INSTANCE;
  }


  protected Retrofit.Builder newRetrofitBuilder() {
    return new Retrofit.Builder();
  }

  private Retrofit retrofit() {
    if (mRetrofit == null) {
      Retrofit.Builder builder = newRetrofitBuilder();
      mRetrofit = builder.baseUrl(sBaseUrl)
          .addConverterFactory(JacksonConverterFactory.create())
          .build();
    }

    return mRetrofit;
  }

  public static TweetService createTweetService() {
    return get().retrofit().create(TweetService.class);
  }
}

编写Fragment继承PagesRecyclerViewFragment

public class FeedFragment extends PagesRecyclerViewFragment<Tweet> {

  private TweetService mTweetService;

  @Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mTweetService = StarterNetwork.createTweetService();
  }

  @Override public void bindViewHolders(EasyRecyclerAdapter adapter) {
    adapter.bind(Tweet.class, TweetViewHolder.class);
  }

  @Override public Call<Collection<Tweet>> paginate(int page, int perPage) {
    return mTweetService.getTweetList(page, perPage);
  }

  @Override public Object getKeyForData(Tweet item) {
    return item.id;
  }
}

Gradle文件

	compile 'com.smartydroid:android-starter-kit:0.0.4'

第三方库

android-starter-kit's People

Contributors

smartydroid avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.