Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Project Structure Complete. A better Main Screen #6

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ dependencies {
compile 'com.android.support.constraint:constraint-layout:1.0.0-alpha8'
compile 'com.squareup.retrofit2:retrofit:2.3.0'
compile 'com.squareup.retrofit2:converter-gson:2.3.0'
compile 'com.squareup.picasso:picasso:2.5.2'
testCompile 'junit:junit:4.12'
}
repositories {
Expand Down
5 changes: 4 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".activities.TodosActivity"></activity>
<activity android:name=".activities.TodosActivity" />
<activity android:name=".activities.AlbumActivity" />
<activity android:name=".activities.LargeImageActivity" />
<activity android:name=".activities.PhotoActivity"></activity>
</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.codingblocks.restapiretrofitjson
import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import com.codingblocks.restapiretrofitjson.activities.AlbumActivity
import com.codingblocks.restapiretrofitjson.activities.PostsActivity
import com.codingblocks.restapiretrofitjson.activities.TodosActivity
import com.codingblocks.restapiretrofitjson.activities.UsersActivity
Expand All @@ -15,18 +16,22 @@ class MainActivity : AppCompatActivity() {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

btnPosts.setOnClickListener({
iv_Posts.setOnClickListener({
startActivity(Intent(this@MainActivity, PostsActivity::class.java))
})

btnUsers.setOnClickListener({
iv_user.setOnClickListener({
startActivity(Intent(this@MainActivity, UsersActivity::class.java))
})

btnTodos.setOnClickListener({
iv_Todos.setOnClickListener({
startActivity(Intent(this@MainActivity, TodosActivity::class.java))
})

iv_Albums.setOnClickListener ({
startActivity(Intent(this@MainActivity,AlbumActivity::class.java))
})


}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.codingblocks.restapiretrofitjson.activities;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.codingblocks.restapiretrofitjson.R;
import com.codingblocks.restapiretrofitjson.adapters.AlbumAdapter;
import com.codingblocks.restapiretrofitjson.api.API;
import com.codingblocks.restapiretrofitjson.api.AlbumAPI;
import com.codingblocks.restapiretrofitjson.interfaces.OnItemClickListener;
import com.codingblocks.restapiretrofitjson.models.Album;

import java.util.ArrayList;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class AlbumActivity extends AppCompatActivity {

public static final String TAG = "alb_act";

RecyclerView rvalbum;
AlbumAdapter albumAdapter;

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

rvalbum = (RecyclerView)findViewById(R.id.rv_album);
rvalbum.setLayoutManager(new LinearLayoutManager(this));

albumAdapter = new AlbumAdapter(this,new ArrayList<Album>());
rvalbum.setAdapter(albumAdapter);

albumAdapter.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(int itemId, View view) {
Intent i = new Intent(AlbumActivity.this,PhotoActivity.class);
i.putExtra("albumId",itemId);
startActivity(i);
}
});

AlbumAPI albumAPI = API.getInstance().getAlbumAPI();

Callback<ArrayList<Album>> albumcallback = new Callback<ArrayList<Album>>() {
@Override
public void onResponse(Call<ArrayList<Album>> call, Response<ArrayList<Album>> response) {
albumAdapter.updateAlbum(response.body());
}

@Override
public void onFailure(Call<ArrayList<Album>> call, Throwable t) {

}
};

int albumIdRecived = getIntent().getIntExtra("userId",-1);
albumAPI.getAlbum().enqueue(albumcallback);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.codingblocks.restapiretrofitjson.activities;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

import com.codingblocks.restapiretrofitjson.R;
import com.squareup.picasso.Picasso;

public class LargeImageActivity extends AppCompatActivity {

ImageView largeimage;
TextView title_largeimage;

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

largeimage = (ImageView) findViewById(R.id.iv_largeimage);
title_largeimage = (TextView) findViewById(R.id.tv_largeimage_title);

String imageUrlRecived = getIntent().getStringExtra("photoURL");
if(imageUrlRecived != null){
Picasso.with(LargeImageActivity.this).load(imageUrlRecived).error(R.drawable.no_image).into(largeimage);
}

String imagetitleRecived = getIntent().getStringExtra("photoTitle");
if(imagetitleRecived != null){
title_largeimage.setText(imagetitleRecived);
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.codingblocks.restapiretrofitjson.activities;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.codingblocks.restapiretrofitjson.R;
import com.codingblocks.restapiretrofitjson.adapters.PhotoAdapter;
import com.codingblocks.restapiretrofitjson.api.API;
import com.codingblocks.restapiretrofitjson.api.PhotoAPI;
import com.codingblocks.restapiretrofitjson.interfaces.OnItemClickListener;
import com.codingblocks.restapiretrofitjson.models.Photo;

import java.util.ArrayList;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class PhotoActivity extends AppCompatActivity {

RecyclerView rvPhotos;
PhotoAdapter photoAdapter;

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

rvPhotos = (RecyclerView) findViewById(R.id.rv_photo);
rvPhotos.setLayoutManager(new LinearLayoutManager(this));

photoAdapter = new PhotoAdapter(this,new ArrayList<Photo>());
rvPhotos.setAdapter(photoAdapter);

photoAdapter.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(int itemId, View view) {
Intent fullImageActIntent=new Intent(PhotoActivity.this,LargeImageActivity.class);
fullImageActIntent.putExtra("photoURL",photoAdapter.getPhotos().get(itemId-1).getUrl());
fullImageActIntent.putExtra("photoTitle",photoAdapter.getPhotos().get(itemId-1).getTitle());
startActivity(fullImageActIntent);
}
});

PhotoAPI photoAPI = API.getInstance().getPhotoAPI();
Callback<ArrayList<Photo>> photocallback = new Callback<ArrayList<Photo>>() {
@Override
public void onResponse(Call<ArrayList<Photo>> call, Response<ArrayList<Photo>> response) {
photoAdapter.updatephotolist(response.body());
}

@Override
public void onFailure(Call<ArrayList<Photo>> call, Throwable t) {

}
};

int albumIdReceived = getIntent().getIntExtra("albumId", -1);
photoAPI.getPhotos().enqueue(photocallback);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@ protected void onCreate(Bundle savedInstanceState) {

rvPostsList = (RecyclerView) findViewById(R.id.rvPostsList);
rvPostsList.setLayoutManager(new LinearLayoutManager(this));

postAdapter = new PostAdapter(this, new ArrayList<Post>());
rvPostsList.setAdapter(postAdapter);

postAdapter.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(int itemId, View view) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package com.codingblocks.restapiretrofitjson.adapters;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.codingblocks.restapiretrofitjson.R;
import com.codingblocks.restapiretrofitjson.interfaces.OnItemClickListener;
import com.codingblocks.restapiretrofitjson.models.Album;

import org.w3c.dom.Text;

import java.util.ArrayList;

/**
* Created by ravneet on 2/7/17.
*/

public class AlbumAdapter extends RecyclerView.Adapter<AlbumAdapter.AlbumViewHolder> {

public static final String TAG = "alb_adp";

private Context context;
private ArrayList<Album> albumlist;
private OnItemClickListener onItemClickListener;

public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
this.onItemClickListener = onItemClickListener;
}

public AlbumAdapter(Context context,ArrayList<Album> albumlist){
this.albumlist = albumlist;
this.context = context;
}

public void updateAlbum(ArrayList<Album> albumlist){
this.albumlist = albumlist;
notifyDataSetChanged();
}

@Override
public AlbumViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

Log.i(TAG, "onCreateViewHolder: ");

LayoutInflater li = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
View itemview = li.inflate(R.layout.list_item_album,parent,false);
return new AlbumViewHolder(itemview);
}

@Override
public void onBindViewHolder(AlbumViewHolder holder, int position) {

Log.i(TAG, "onBindViewHolder: ");

final Album thisalbum = albumlist.get(position);

holder.albumTitle.setText(thisalbum.getTitle());
holder.thisview.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(onItemClickListener != null){
onItemClickListener.onItemClick(thisalbum.getId(),view);
}
}
});

}

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

class AlbumViewHolder extends RecyclerView.ViewHolder{
TextView albumTitle;
View thisview;

public AlbumViewHolder(View itemView) {
super(itemView);
thisview = itemView;
albumTitle = (TextView) itemView.findViewById(R.id.tv_album_title);

}
}
}
Loading