123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- package com.example.mytest;
- import android.app.Activity;
- import android.graphics.Bitmap;
- import android.graphics.Color;
- import android.os.Bundle;
- import android.text.TextUtils;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.Button;
- import android.widget.ImageView;
- import android.widget.LinearLayout;
- import android.widget.TextView;
- import org.json.JSONArray;
- import org.json.JSONObject;
- import org.w3c.dom.Text;
- public class MainActivity extends Activity implements Interface {
- private Interface context;
- @Override
- public void Main(final JSONArray array) {
- runOnUiThread(new Runnable() {
- @Override
- public void run() {
- LinearLayout news = findViewById(R.id.newContainer);
- try {
- for (int i = array.length()-1; i >= 0; i--) {
- JSONObject o = array.getJSONObject(i);
- // Контейнер под 1 новость
- LinearLayout newsContainer = new LinearLayout(getApplicationContext());
- LinearLayout.LayoutParams newsContainerLayout =
- new LinearLayout.LayoutParams(
- LinearLayout.LayoutParams.MATCH_PARENT,
- LinearLayout.LayoutParams.WRAP_CONTENT);
- newsContainerLayout.bottomMargin = 20;
- newsContainer.setLayoutParams(newsContainerLayout);
- newsContainer.setBackgroundColor(Color.WHITE);
- newsContainer.setOrientation(LinearLayout.VERTICAL);
- // Заголовок новости
- TextView caption = new TextView(getApplicationContext());
- LinearLayout.LayoutParams captionLayout =
- new LinearLayout.LayoutParams(
- LinearLayout.LayoutParams.MATCH_PARENT,
- LinearLayout.LayoutParams.WRAP_CONTENT);
- captionLayout.setMargins(20, 20, 20, 20);
- caption.setLayoutParams(captionLayout);
- caption.setTextSize(20);
- caption.setText(o.getString("Caption"));
- caption.setPadding(0, 20, 0, 20);
- caption.setTextColor(Color.rgb(50,50,50));
- newsContainer.addView(caption);
- // Текст новости
- TextView preview = new TextView(getApplicationContext());
- LinearLayout.LayoutParams previewLayout =
- new LinearLayout.LayoutParams(
- LinearLayout.LayoutParams.MATCH_PARENT,
- LinearLayout.LayoutParams.WRAP_CONTENT
- );
- previewLayout.setMargins(20, 50, 20, 20);
- preview.setLayoutParams(previewLayout);
- preview.setText(o.getString("Preview"));
- preview.setTextColor(Color.BLACK);
- preview.setMaxLines(6);
- preview.setEllipsize(TextUtils.TruncateAt.END);
- newsContainer.addView(preview);
- ImageView image = new ImageView(getApplicationContext());
- LinearLayout.LayoutParams imageLayout =
- new LinearLayout.LayoutParams(
- LinearLayout.LayoutParams.MATCH_PARENT,
- 600
- );
- image.setLayoutParams(imageLayout);
- image.setScaleType(ImageView.ScaleType.CENTER_CROP);
- newsContainer.addView(image);
- new ImageDownload(o.getString("Image"), image, context).start();
- news.addView(newsContainer);
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- });
- }
- @Override
- public void SetImage(final ImageView img, final Bitmap src) {
- runOnUiThread(new Runnable() {
- @Override
- public void run() {
- img.setImageBitmap(src);
- }
- });
- }
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- context = this;
- Button button = findViewById(R.id.button);
- button.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- API api = new API();
- api.url = "https:/ktk-45.ru/news";
- api.param = "{\"Page\": 0,\"Limit\": 10}";
- api.context = context;
- api.start();
- }
- });
- }
- }
|