Thursday 31 December 2015

Android basic function for service

        @Override
public void onStart(Intent intent, int startId) {
handleStart(intent, startId);

}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
handleStart(intent, startId);

// Only action ACTION_REFRESH creates background thread, so just return
// START_NOT_STICKY
return START_REDELIVER_INTENT;
}

private void handleStart(Intent intent, int startId) {

if (intent != null) {
               }
}

Sunday 20 December 2015

RecycleView with mutiple Layout

public class MainAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private ArrayList<Object> lstCommentsReply ;
    private static final int TYPE_COMMENT = 0;
    private static final int TYPE_REPLY = 1;

    public class ReplyViewHolder extends RecyclerView.ViewHolder {
        TextView mNotes;

        public ReplyViewHolder(View itemView) {
            super(itemView);
            mNotes=(TextView)itemView.findViewById(R.id.tv_notes);
        }
    }

    public class CommentViewHolder extends RecyclerView.ViewHolder {
        TextView mNotes;

        public CommentViewHolder(View itemView) {
            super(itemView);
            mNotes=(TextView)itemView.findViewById(R.id.tv_notes);

        }
    }
    
    @Override    public int getItemViewType(int position) {
        int viewType;
        if (lstCommentsReply.get(position) instanceof Comment) {
            viewType = TYPE_COMMENT;
        } else {
            viewType = TYPE_REPLY;
        }
        return viewType;
    }

    @Override    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        LayoutInflater mInflater = LayoutInflater.from(viewGroup.getContext());
        switch (viewType) {

            case TYPE_COMMENT:
                ViewGroup vImage = (ViewGroup) mInflater.inflate(R.layout.list_item_comment, viewGroup, false);
                CommentViewHolder vhComments = new CommentViewHolder(vImage);
                return vhComments;
            case TYPE_REPLY:
                ViewGroup vGroup = (ViewGroup) mInflater.inflate(R.layout.list_item_reply, viewGroup, false);
                ReplyViewHolder vhReply = new ReplyViewHolder(vGroup);
                return vhReply;
            default:
                ViewGroup vGroup0 = (ViewGroup) mInflater.inflate(R.layout.list_item_comment, viewGroup, false);
                ReplyViewHolder vhCommentsDefault = new ReplyViewHolder(vGroup0);
                return vhCommentsDefault;
        }
    }

    @Override    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int i) {
        switch (viewHolder.getItemViewType()) {

            case TYPE_COMMENT:

                Comment comment = (Comment) lstCommentsReply.get(i);

                CommentViewHolder commentViewHolder = (CommentViewHolder) viewHolder;

                commentViewHolder.mNotes.setText(comment.getCommentText());
                break;

            case TYPE_REPLY:
                Reply reply = (Reply) lstCommentsReply.get(i);
                ReplyViewHolder replyViewHolder = (ReplyViewHolder) viewHolder;
                replyViewHolder.mNotes.setText(reply.getReplyText());
                break;

        }
    }

    @Override    public int getItemCount() {
        return lstCommentsReply.size();
    }

    public MainAdapter(List<Object> commentsReplyList) {

        this.lstCommentsReply = new ArrayList<Object>(commentsReplyList);
    }
}


// Create and Set Adpater

Reply reply;
Comment comment;
mLstCommentsReply=new ArrayList<Object>();
comment=new Comment();
comment.setCommentText("Comment1");
mLstCommentsReply.add(comment);
comment=new Comment();
comment.setCommentText("Comment2");
mLstCommentsReply.add(comment);
reply=new Reply();
reply.setReplyText("Reply1");
mLstCommentsReply.add(reply);
for(int i=0;i<20;i++) {
    comment = new Comment();
    comment.setCommentText("Comment3");
    mLstCommentsReply.add(comment);
    reply = new Reply();
    reply.setReplyText("Reply2");
    mLstCommentsReply.add(reply);
}
RecyclerView  mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this,
        LinearLayoutManager.VERTICAL, false);
mRecyclerView.setLayoutManager(linearLayoutManager);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setAdapter(new MainAdapter(mLstCommentsReply));



//   item layout

list_item_comment.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="@color/university_background"    android:orientation="vertical">

    <TextView        android:layout_width="match_parent" android:id="@+id/tv_notes" android:textColor="@color/white"        android:layout_height="wrap_content" />

</LinearLayout>


list_item_replay.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="@color/communityname_background"    android:orientation="vertical">

    <TextView        android:layout_width="match_parent" android:id="@+id/tv_notes"        android:layout_height="wrap_content" />

</LinearLayout>